diff --git a/Readme.md b/Readme.md index 2626956..006c9b5 100644 --- a/Readme.md +++ b/Readme.md @@ -1,12 +1,17 @@ Boost libraries - trimmed down for Citra ======================================== +This is a subset of Boost v1.57.0. + Currently imported libraries: ----------------------------- * concept * config +* container * core +* intrusive * iterator +* move * mpl * preprocessor * range diff --git a/boost/container/adaptive_pool.hpp b/boost/container/adaptive_pool.hpp new file mode 100644 index 0000000..c12d511 --- /dev/null +++ b/boost/container/adaptive_pool.hpp @@ -0,0 +1,349 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP +#define BOOST_CONTAINER_ADAPTIVE_POOL_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + + +namespace boost { +namespace container { + +//!An STL node allocator that uses a modified DLMalloc as memory +//!source. +//! +//!This node allocator shares a segregated storage between all instances +//!of adaptive_pool with equal sizeof(T). +//! +//!NodesPerBlock is the number of nodes allocated at once when the allocator +//!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks +//!that the adaptive node pool will hold. The rest of the totally free blocks will be +//!deallocated to the memory manager. +//! +//!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator: +//!(memory usable for nodes / total memory allocated from the memory allocator) +template < class T + , std::size_t NodesPerBlock BOOST_CONTAINER_DOCONLY(= ADP_nodes_per_block) + , std::size_t MaxFreeBlocks BOOST_CONTAINER_DOCONLY(= ADP_max_free_blocks) + , std::size_t OverheadPercent BOOST_CONTAINER_DOCONLY(= ADP_overhead_percent) + BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I unsigned Version) + > +class adaptive_pool +{ + //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2, + //!the allocator offers advanced expand in place and burst allocation capabilities. + public: + typedef unsigned int allocation_type; + typedef adaptive_pool + self_t; + + static const std::size_t nodes_per_block = NodesPerBlock; + static const std::size_t max_free_blocks = MaxFreeBlocks; + static const std::size_t overhead_percent = OverheadPercent; + static const std::size_t real_nodes_per_block = NodesPerBlock; + + BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2))); + + public: + //------- + typedef T value_type; + typedef T * pointer; + typedef const T * const_pointer; + typedef typename ::boost::container:: + container_detail::unvoid::type & reference; + typedef const typename ::boost::container:: + container_detail::unvoid::type & const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + typedef boost::container::container_detail:: + version_type version; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef boost::container::container_detail:: + basic_multiallocation_chain multiallocation_chain_void; + typedef boost::container::container_detail:: + transform_multiallocation_chain + multiallocation_chain; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //!Obtains adaptive_pool from + //!adaptive_pool + template + struct rebind + { + typedef adaptive_pool + < T2 + , NodesPerBlock + , MaxFreeBlocks + , OverheadPercent + BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I Version) + > other; + }; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + //!Not assignable from related adaptive_pool + template + adaptive_pool& operator= + (const adaptive_pool&); + + //!Not assignable from other adaptive_pool + adaptive_pool& operator=(const adaptive_pool&); + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + //!Default constructor + adaptive_pool() BOOST_CONTAINER_NOEXCEPT + {} + + //!Copy constructor from other adaptive_pool. + adaptive_pool(const adaptive_pool &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Copy constructor from related adaptive_pool. + template + adaptive_pool + (const adaptive_pool &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Destructor + ~adaptive_pool() BOOST_CONTAINER_NOEXCEPT + {} + + //!Returns the number of elements that could be allocated. + //!Never throws + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return size_type(-1)/sizeof(T); } + + //!Allocate memory for an array of count elements. + //!Throws std::bad_alloc if there is no enough memory + pointer allocate(size_type count, const void * = 0) + { + if(count > this->max_size()) + boost::container::throw_bad_alloc(); + + if(Version == 1 && count == 1){ + typedef typename container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + return pointer(static_cast(singleton_t::instance().allocate_node())); + } + else{ + return static_cast(boost_cont_malloc(count*sizeof(T))); + } + } + + //!Deallocate allocated memory. + //!Never throws + void deallocate(const pointer &ptr, size_type count) BOOST_CONTAINER_NOEXCEPT + { + (void)count; + if(Version == 1 && count == 1){ + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_node(ptr); + } + else{ + boost_cont_free(ptr); + } + } + + std::pair + allocation_command(allocation_type command, + size_type limit_size, + size_type preferred_size, + size_type &received_size, pointer reuse = pointer()) + { + std::pair ret = + this->priv_allocation_command(command, limit_size, preferred_size, received_size, reuse); + if(!ret.first && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)) + boost::container::throw_bad_alloc(); + return ret; + } + + //!Returns maximum the number of objects the previously allocated memory + //!pointed by p can hold. + size_type size(pointer p) const BOOST_CONTAINER_NOEXCEPT + { return boost_cont_size(p); } + + //!Allocates just one object. Memory allocated with this function + //!must be deallocated only with deallocate_one(). + //!Throws bad_alloc if there is no enough memory + pointer allocate_one() + { + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + return (pointer)singleton_t::instance().allocate_node(); + } + + //!Allocates many elements of size == 1. + //!Elements must be individually deallocated with deallocate_one() + void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) + { + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().allocate_nodes(num_elements, static_cast(chain)); + //typename shared_pool_t::multiallocation_chain ch; + //singleton_t::instance().allocate_nodes(num_elements, ch); + //chain.incorporate_after + //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size()); + } + + //!Deallocates memory previously allocated with allocate_one(). + //!You should never use deallocate_one to deallocate memory allocated + //!with other functions different from allocate_one(). Never throws + void deallocate_one(pointer p) BOOST_CONTAINER_NOEXCEPT + { + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_node(p); + } + + void deallocate_individual(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + { + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size()); + //singleton_t::instance().deallocate_nodes(ch); + singleton_t::instance().deallocate_nodes(chain); + } + + //!Allocates many elements of size elem_size. + //!Elements must be individually deallocated with deallocate() + void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 ));/* + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT(&ch); + if(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after(chain.before_begin() + ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) + ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ + if(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast(&chain))){ + boost::container::throw_bad_alloc(); + } + } + + //!Allocates n_elements elements, each one of size elem_sizes[i] + //!Elements must be individually deallocated with deallocate() + void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 ));/* + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT(&ch); + if(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after(chain.before_begin() + ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) + ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ + if(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast(&chain))){ + boost::container::throw_bad_alloc(); + } + } + + void deallocate_many(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + {/* + boost_cont_memchain ch; + void *beg(&*chain.begin()), *last(&*chain.last()); + size_t size(chain.size()); + BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size); + boost_cont_multidealloc(&ch);*/ + boost_cont_multidealloc(reinterpret_cast(&chain)); + } + + //!Deallocates all free blocks of the pool + static void deallocate_free_blocks() BOOST_CONTAINER_NOEXCEPT + { + typedef container_detail::shared_adaptive_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_free_blocks(); + } + + //!Swaps allocators. Does not throw. If each allocator is placed in a + //!different memory segment, the result is undefined. + friend void swap(adaptive_pool &, adaptive_pool &) BOOST_CONTAINER_NOEXCEPT + {} + + //!An allocator always compares to true, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_CONTAINER_NOEXCEPT + { return true; } + + //!An allocator always compares to false, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_CONTAINER_NOEXCEPT + { return false; } + + private: + std::pair priv_allocation_command + (allocation_type command, std::size_t limit_size + ,std::size_t preferred_size,std::size_t &received_size, void *reuse_ptr) + { + boost_cont_command_ret_t ret = {0 , 0}; + if(limit_size > this->max_size() || preferred_size > this->max_size()){ +// ret.first = 0; + return std::pair(pointer(), false); + } + std::size_t l_size = limit_size*sizeof(T); + std::size_t p_size = preferred_size*sizeof(T); + std::size_t r_size; + { + ret = boost_cont_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr); + } + received_size = r_size/sizeof(T); + return std::pair(static_cast(ret.first), !!ret.second); + } +}; + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP diff --git a/boost/container/allocator.hpp b/boost/container/allocator.hpp new file mode 100644 index 0000000..14d5645 --- /dev/null +++ b/boost/container/allocator.hpp @@ -0,0 +1,367 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATOR_HPP +#define BOOST_CONTAINER_ALLOCATOR_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +template +class allocator +{ + typedef allocator self_t; + public: + typedef void value_type; + typedef void * pointer; + typedef const void* const_pointer; + typedef int & reference; + typedef const int & const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef boost::container::container_detail:: + version_type version; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef boost::container::container_detail:: + basic_multiallocation_chain multiallocation_chain; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //!Obtains an allocator that allocates + //!objects of type T2 + template + struct rebind + { + typedef allocator< T2 + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + , Version, AllocationDisableMask + #endif + > other; + }; + + //!Default constructor + //!Never throws + allocator() + {} + + //!Constructor from other allocator. + //!Never throws + allocator(const allocator &) + {} + + //!Constructor from related allocator. + //!Never throws + template + allocator(const allocator &) + {} +}; + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//!\file +//! This class is an extended STL-compatible that offers advanced allocation mechanism +//!(in-place expansion, shrinking, burst-allocation...) +//! +//! This allocator is a wrapper around a modified DLmalloc. +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template +#else +//! If Version is 1, the allocator is a STL conforming allocator. If Version is 2, +//! the allocator offers advanced expand in place and burst allocation capabilities. +// +//! AllocationDisableMask works only if Version is 2 and it can be an inclusive OR +//! of allocation types the user wants to disable. +template +#endif //#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +class allocator +{ + typedef unsigned int allocation_type; + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + + //Self type + typedef allocator self_t; + + //Not assignable from related allocator + template + allocator& operator=(const allocator&); + + //Not assignable from other allocator + allocator& operator=(const allocator&); + + static const unsigned int ForbiddenMask = + BOOST_CONTAINER_ALLOCATE_NEW | BOOST_CONTAINER_EXPAND_BWD | BOOST_CONTAINER_EXPAND_FWD ; + + //The mask can't disable all the allocation types + BOOST_STATIC_ASSERT(( (AllocationDisableMask & ForbiddenMask) != ForbiddenMask )); + + //The mask is only valid for version 2 allocators + BOOST_STATIC_ASSERT(( Version != 1 || (AllocationDisableMask == 0) )); + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + typedef T value_type; + typedef T * pointer; + typedef const T * const_pointer; + typedef T & reference; + typedef const T & const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + typedef boost::container::container_detail:: + version_type version; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef boost::container::container_detail:: + basic_multiallocation_chain void_multiallocation_chain; + + typedef boost::container::container_detail:: + transform_multiallocation_chain + multiallocation_chain; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //!Obtains an allocator that allocates + //!objects of type T2 + template + struct rebind + { + typedef allocator other; + }; + + //!Default constructor + //!Never throws + allocator() BOOST_CONTAINER_NOEXCEPT + {} + + //!Constructor from other allocator. + //!Never throws + allocator(const allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Constructor from related allocator. + //!Never throws + template + allocator(const allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Allocates memory for an array of count elements. + //!Throws std::bad_alloc if there is no enough memory + //!If Version is 2, this allocated memory can only be deallocated + //!with deallocate() or (for Version == 2) deallocate_many() + pointer allocate(size_type count, const void * hint= 0) + { + (void)hint; + if(count > this->max_size()) + boost::container::throw_bad_alloc(); + void *ret = boost_cont_malloc(count*sizeof(T)); + if(!ret) + boost::container::throw_bad_alloc(); + return static_cast(ret); + } + + //!Deallocates previously allocated memory. + //!Never throws + void deallocate(pointer ptr, size_type) BOOST_CONTAINER_NOEXCEPT + { boost_cont_free(ptr); } + + //!Returns the maximum number of elements that could be allocated. + //!Never throws + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return size_type(-1)/sizeof(T); } + + //!Swaps two allocators, does nothing + //!because this allocator is stateless + friend void swap(self_t &, self_t &) BOOST_CONTAINER_NOEXCEPT + {} + + //!An allocator always compares to true, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator==(const allocator &, const allocator &) BOOST_CONTAINER_NOEXCEPT + { return true; } + + //!An allocator always compares to false, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator!=(const allocator &, const allocator &) BOOST_CONTAINER_NOEXCEPT + { return false; } + + //!An advanced function that offers in-place expansion shrink to fit and new allocation + //!capabilities. Memory allocated with this function can only be deallocated with deallocate() + //!or deallocate_many(). + //!This function is available only with Version == 2 + std::pair + allocation_command(allocation_type command, + size_type limit_size, + size_type preferred_size, + size_type &received_size, pointer reuse = pointer()) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + const allocation_type mask(AllocationDisableMask); + command &= ~mask; + std::pair ret = + priv_allocation_command(command, limit_size, preferred_size, received_size, reuse); + if(!ret.first && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)) + boost::container::throw_bad_alloc(); + return ret; + } + + //!Returns maximum the number of objects the previously allocated memory + //!pointed by p can hold. + //!Memory must not have been allocated with + //!allocate_one or allocate_individual. + //!This function is available only with Version == 2 + size_type size(pointer p) const BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + return boost_cont_size(p); + } + + //!Allocates just one object. Memory allocated with this function + //!must be deallocated only with deallocate_one(). + //!Throws bad_alloc if there is no enough memory + //!This function is available only with Version == 2 + pointer allocate_one() + { + BOOST_STATIC_ASSERT(( Version > 1 )); + return this->allocate(1); + } + + //!Allocates many elements of size == 1. + //!Elements must be individually deallocated with deallocate_one() + //!This function is available only with Version == 2 + void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + this->allocate_many(1, num_elements, chain); + } + + //!Deallocates memory previously allocated with allocate_one(). + //!You should never use deallocate_one to deallocate memory allocated + //!with other functions different from allocate_one() or allocate_individual. + //Never throws + void deallocate_one(pointer p) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + return this->deallocate(p, 1); + } + + //!Deallocates memory allocated with allocate_one() or allocate_individual(). + //!This function is available only with Version == 2 + void deallocate_individual(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + return this->deallocate_many(chain); + } + + //!Allocates many elements of size elem_size. + //!Elements must be individually deallocated with deallocate() + //!This function is available only with Version == 2 + void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 ));/* + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT(&ch); + if(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after(chain.before_begin() + ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) + ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ + if(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast(&chain))){ + boost::container::throw_bad_alloc(); + } + } + + //!Allocates n_elements elements, each one of size elem_sizes[i] + //!Elements must be individually deallocated with deallocate() + //!This function is available only with Version == 2 + void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT(&ch); + if(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after(chain.before_begin() + ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) + ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) ); + /* + if(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast(&chain))){ + boost::container::throw_bad_alloc(); + }*/ + } + + //!Deallocates several elements allocated by + //!allocate_many(), allocate(), or allocation_command(). + //!This function is available only with Version == 2 + void deallocate_many(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + boost_cont_memchain ch; + void *beg(&*chain.begin()), *last(&*chain.last()); + size_t size(chain.size()); + BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size); + boost_cont_multidealloc(&ch); + //boost_cont_multidealloc(reinterpret_cast(&chain)); + } + + private: + + std::pair priv_allocation_command + (allocation_type command, std::size_t limit_size + ,std::size_t preferred_size,std::size_t &received_size, void *reuse_ptr) + { + boost_cont_command_ret_t ret = {0 , 0}; + if((limit_size > this->max_size()) | (preferred_size > this->max_size())){ + return std::pair(pointer(), false); + } + std::size_t l_size = limit_size*sizeof(T); + std::size_t p_size = preferred_size*sizeof(T); + std::size_t r_size; + { + ret = boost_cont_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr); + } + received_size = r_size/sizeof(T); + return std::pair(static_cast(ret.first), !!ret.second); + } +}; + +} //namespace container { +} //namespace boost { + +#include + +#endif //BOOST_CONTAINER_ALLOCATOR_HPP + diff --git a/boost/container/allocator_traits.hpp b/boost/container/allocator_traits.hpp new file mode 100644 index 0000000..a85831f --- /dev/null +++ b/boost/container/allocator_traits.hpp @@ -0,0 +1,409 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Pablo Halpern 2009. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP +#define BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include +#endif + + + +namespace boost { +namespace container { +namespace allocator_traits_detail { + +} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace container_detail { + +//workaround needed for C++03 compilers with no construct() +//supporting rvalue references +template +struct is_std_allocator +{ static const bool value = false; }; + +template +struct is_std_allocator< std::allocator > +{ static const bool value = true; }; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! The class template allocator_traits supplies a uniform interface to all allocator types. +//! This class is a C++03-compatible implementation of std::allocator_traits +template +struct allocator_traits +{ + //allocator_type + typedef Alloc allocator_type; + //value_type + typedef typename Alloc::value_type value_type; + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Alloc::pointer if such a type exists; otherwise, value_type* + //! + typedef unspecified pointer; + //! Alloc::const_pointer if such a type exists ; otherwise, pointer_traits::rebind::rebind. + //! + typedef see_documentation void_pointer; + //! Alloc::const_void_pointer if such a type exists ; otherwis e, pointer_traits::rebind::difference_type. + //! + typedef see_documentation difference_type; + //! Alloc::size_type if such a type exists ; otherwise, make_unsigned::type + //! + typedef see_documentation size_type; + //! Alloc::propagate_on_container_copy_assignment if such a type exists, otherwise an integral_constant + //! type with internal constant static member value == false. + typedef see_documentation propagate_on_container_copy_assignment; + //! Alloc::propagate_on_container_move_assignment if such a type exists, otherwise an integral_constant + //! type with internal constant static member value == false. + typedef see_documentation propagate_on_container_move_assignment; + //! Alloc::propagate_on_container_swap if such a type exists, otherwise an integral_constant + //! type with internal constant static member value == false. + typedef see_documentation propagate_on_container_swap; + //! Defines an allocator: Alloc::rebind::other if such a type exists; otherwise, Alloc + //! if Alloc is a class template instantiation of the form Alloc, where Args is zero or + //! more type arguments ; otherwise, the instantiation of rebind_alloc is ill-formed. + //! + //! In C++03 compilers rebind_alloc is a struct derived from an allocator + //! deduced by previously detailed rules. + template using rebind_alloc = see_documentation; + + //! In C++03 compilers rebind_traits is a struct derived from + //! allocator_traits, where OtherAlloc is + //! the allocator deduced by rules explained in rebind_alloc. + template using rebind_traits = allocator_traits >; + + //! Non-standard extension: Portable allocator rebind for C++03 and C++11 compilers. + //! type is an allocator related to Alloc deduced deduced by rules explained in rebind_alloc. + template + struct portable_rebind_alloc + { typedef see_documentation type; }; + #else + //pointer + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + pointer, value_type*) + pointer; + //const_pointer + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(boost::container::container_detail::, Alloc, + const_pointer, typename boost::intrusive::pointer_traits::template + rebind_pointer) + const_pointer; + //reference + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + reference, typename container_detail::unvoid::type&) + reference; + //const_reference + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + const_reference, const typename container_detail::unvoid::type&) + const_reference; + //void_pointer + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(boost::container::container_detail::, Alloc, + void_pointer, typename boost::intrusive::pointer_traits::template + rebind_pointer) + void_pointer; + //const_void_pointer + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(boost::container::container_detail::, Alloc, + const_void_pointer, typename boost::intrusive::pointer_traits::template + rebind_pointer) + const_void_pointer; + //difference_type + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + difference_type, std::ptrdiff_t) + difference_type; + //size_type + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + size_type, std::size_t) + size_type; + //propagate_on_container_copy_assignment + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + propagate_on_container_copy_assignment, container_detail::false_type) + propagate_on_container_copy_assignment; + //propagate_on_container_move_assignment + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + propagate_on_container_move_assignment, container_detail::false_type) + propagate_on_container_move_assignment; + //propagate_on_container_swap + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, Alloc, + propagate_on_container_swap, container_detail::false_type) + propagate_on_container_swap; + + #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + //C++11 + template using rebind_alloc = typename boost::intrusive::pointer_rebind::type; + template using rebind_traits = allocator_traits< rebind_alloc >; + #else // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + //Some workaround for C++03 or C++11 compilers with no template aliases + template + struct rebind_alloc : boost::intrusive::pointer_rebind::type + { + typedef typename boost::intrusive::pointer_rebind::type Base; + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + template + rebind_alloc(BOOST_FWD_REF(Args)... args) + : Base(boost::forward(args)...) + {} + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + rebind_alloc(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + : Base(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)) \ + {} \ + // + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + }; + + template + struct rebind_traits + : allocator_traits::type> + {}; + #endif // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + template + struct portable_rebind_alloc + { typedef typename boost::intrusive::pointer_rebind::type type; }; + #endif //BOOST_CONTAINER_DOXYGEN_INVOKED + + //! Returns: a.allocate(n) + //! + static pointer allocate(Alloc &a, size_type n) + { return a.allocate(n); } + + //! Returns: a.deallocate(p, n) + //! + //! Throws: Nothing + static void deallocate(Alloc &a, pointer p, size_type n) + { a.deallocate(p, n); } + + //! Effects: calls a.allocate(n, p) if that call is well-formed; + //! otherwise, invokes a.allocate(n) + static pointer allocate(Alloc &a, size_type n, const_void_pointer p) + { + const bool value = boost::container::container_detail:: + has_member_function_callable_with_allocate + ::value; + container_detail::bool_ flag; + return allocator_traits::priv_allocate(flag, a, n, p); + } + + //! Effects: calls a.destroy(p) if that call is well-formed; + //! otherwise, invokes p->~T(). + template + static void destroy(Alloc &a, T*p) BOOST_CONTAINER_NOEXCEPT + { + typedef T* destroy_pointer; + const bool value = boost::container::container_detail:: + has_member_function_callable_with_destroy + ::value; + container_detail::bool_ flag; + allocator_traits::priv_destroy(flag, a, p); + } + + //! Returns: a.max_size() if that expression is well-formed; otherwise, + //! numeric_limits::max(). + static size_type max_size(const Alloc &a) BOOST_CONTAINER_NOEXCEPT + { + const bool value = boost::container::container_detail:: + has_member_function_callable_with_max_size + ::value; + container_detail::bool_ flag; + return allocator_traits::priv_max_size(flag, a); + } + + //! Returns: a.select_on_container_copy_construction() if that expression is well-formed; + //! otherwise, a. + static + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + typename container_detail::if_c + < boost::container::container_detail:: + has_member_function_callable_with_select_on_container_copy_construction + ::value + , Alloc + , const Alloc & + >::type + #else + Alloc + #endif + select_on_container_copy_construction(const Alloc &a) + { + const bool value = boost::container::container_detail:: + has_member_function_callable_with_select_on_container_copy_construction + ::value; + container_detail::bool_ flag; + return allocator_traits::priv_select_on_container_copy_construction(flag, a); + } + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: calls a.construct(p, std::forward(args)...) if that call is well-formed; + //! otherwise, invokes ::new (static_cast(p)) T(std::forward(args)...) + template + static void construct(Alloc & a, T* p, BOOST_FWD_REF(Args)... args) + { + container_detail::bool_::value> flag; + allocator_traits::priv_construct(flag, a, p, ::boost::forward(args)...); + } + #endif + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + private: + static pointer priv_allocate(container_detail::true_type, Alloc &a, size_type n, const_void_pointer p) + { return a.allocate(n, p); } + + static pointer priv_allocate(container_detail::false_type, Alloc &a, size_type n, const_void_pointer) + { return allocator_traits::allocate(a, n); } + + template + static void priv_destroy(container_detail::true_type, Alloc &a, T* p) BOOST_CONTAINER_NOEXCEPT + { a.destroy(p); } + + template + static void priv_destroy(container_detail::false_type, Alloc &, T* p) BOOST_CONTAINER_NOEXCEPT + { p->~T(); (void)p; } + + static size_type priv_max_size(container_detail::true_type, const Alloc &a) BOOST_CONTAINER_NOEXCEPT + { return a.max_size(); } + + static size_type priv_max_size(container_detail::false_type, const Alloc &) BOOST_CONTAINER_NOEXCEPT + { return size_type(-1); } + + static Alloc priv_select_on_container_copy_construction(container_detail::true_type, const Alloc &a) + { return a.select_on_container_copy_construction(); } + + static const Alloc &priv_select_on_container_copy_construction(container_detail::false_type, const Alloc &a) BOOST_CONTAINER_NOEXCEPT + { return a; } + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + template + static void priv_construct(container_detail::false_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args) + { + const bool value = boost::container::container_detail:: + has_member_function_callable_with_construct + < Alloc, T*, Args... >::value; + container_detail::bool_ flag; + priv_construct_dispatch2(flag, a, p, ::boost::forward(args)...); + } + + template + static void priv_construct(container_detail::true_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args) + { + priv_construct_dispatch2(container_detail::false_type(), a, p, ::boost::forward(args)...); + } + + template + static void priv_construct_dispatch2(container_detail::true_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args) + { a.construct( p, ::boost::forward(args)...); } + + template + static void priv_construct_dispatch2(container_detail::false_type, Alloc &, T *p, BOOST_FWD_REF(Args) ...args) + { ::new((void*)p, boost_container_new_t()) T(::boost::forward(args)...); } + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + public: + #define BOOST_PP_LOCAL_MACRO(n) \ + template \ + static void construct(Alloc &a, T *p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + container_detail::bool_ \ + ::value> flag; \ + allocator_traits::priv_construct(flag, a, p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + // + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + private: + #define BOOST_PP_LOCAL_MACRO(n) \ + template \ + static void priv_construct(container_detail::false_type, Alloc &a, T *p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \ + { \ + const bool value = \ + boost::container::container_detail::has_member_function_callable_with_construct \ + < Alloc, T* BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_FWD_TYPE, _) >::value; \ + container_detail::bool_ flag; \ + priv_construct_dispatch2(flag, a, p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + } \ + \ + template \ + static void priv_construct(container_detail::true_type, Alloc &a, T *p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \ + { \ + priv_construct_dispatch2(container_detail::false_type(), a, p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + } \ + \ + template \ + static void priv_construct_dispatch2(container_detail::true_type, Alloc &a, T *p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \ + { a.construct( p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); } \ + \ + template \ + static void priv_construct_dispatch2(container_detail::false_type, Alloc &, T *p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \ + { ::new((void*)p, boost_container_new_t()) T(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); }\ + // + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template + static void priv_construct_dispatch2(container_detail::false_type, Alloc &, T *p, ::boost::container::default_init_t) + { ::new((void*)p) T; } + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +} //namespace container { +} //namespace boost { + +#include + +#endif // ! defined(BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP) diff --git a/boost/container/container_fwd.hpp b/boost/container/container_fwd.hpp new file mode 100644 index 0000000..20ac778 --- /dev/null +++ b/boost/container/container_fwd.hpp @@ -0,0 +1,270 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_CONTAINER_FWD_HPP +#define BOOST_CONTAINER_CONTAINER_FWD_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +//! \file +//! This header file forward declares the following containers: +//! - boost::container::vector +//! - boost::container::stable_vector +//! - boost::container::static_vector +//! - boost::container::slist +//! - boost::container::list +//! - boost::container::set +//! - boost::container::multiset +//! - boost::container::map +//! - boost::container::multimap +//! - boost::container::flat_set +//! - boost::container::flat_multiset +//! - boost::container::flat_map +//! - boost::container::flat_multimap +//! - boost::container::basic_string +//! - boost::container::string +//! - boost::container::wstring +//! +//! It forward declares the following allocators: +//! - boost::container::allocator +//! - boost::container::node_allocator +//! - boost::container::adaptive_pool +//! +//! And finally it defines the following types + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//Std forward declarations +#ifndef BOOST_CONTAINER_DETAIL_STD_FWD_HPP + #include +#endif + +namespace boost{ +namespace intrusive{ + //Create namespace to avoid compilation errors +}} + +namespace boost{ namespace container{ namespace container_detail{ + namespace bi = boost::intrusive; +}}} + +#include + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +////////////////////////////////////////////////////////////////////////////// +// Containers +////////////////////////////////////////////////////////////////////////////// + +namespace boost { +namespace container { + +//! Enumeration used to configure ordered associative containers +//! with a concrete tree implementation. +enum tree_type_enum +{ + red_black_tree, + avl_tree, + scapegoat_tree, + splay_tree +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +template > +class vector; + +template > +class stable_vector; + +template +class static_vector; + +template > +class deque; + +template > +class list; + +template > +class slist; + +template +struct tree_opt; + +typedef tree_opt tree_assoc_defaults; + +template + ,class Allocator = std::allocator + ,class Options = tree_assoc_defaults > +class set; + +template + ,class Allocator = std::allocator + ,class Options = tree_assoc_defaults > +class multiset; + +template + ,class Allocator = std::allocator > + ,class Options = tree_assoc_defaults > +class map; + +template + ,class Allocator = std::allocator > + ,class Options = tree_assoc_defaults > +class multimap; + +template + ,class Allocator = std::allocator > +class flat_set; + +template + ,class Allocator = std::allocator > +class flat_multiset; + +template + ,class Allocator = std::allocator > > +class flat_map; + +template + ,class Allocator = std::allocator > > +class flat_multimap; + +template + ,class Allocator = std::allocator > +class basic_string; + +typedef basic_string + + ,std::allocator > +string; + +typedef basic_string + + ,std::allocator > +wstring; + +static const std::size_t ADP_nodes_per_block = 256u; +static const std::size_t ADP_max_free_blocks = 2u; +static const std::size_t ADP_overhead_percent = 1u; +static const std::size_t ADP_only_alignment = 0u; + +template < class T + , std::size_t NodesPerBlock = ADP_nodes_per_block + , std::size_t MaxFreeBlocks = ADP_max_free_blocks + , std::size_t OverheadPercent = ADP_overhead_percent + , unsigned Version = 2 + > +class adaptive_pool; + +template < class T + , unsigned Version = 2 + , unsigned int AllocationDisableMask = 0> +class allocator; + +static const std::size_t NodeAlloc_nodes_per_block = 256u; + +template + < class T + , std::size_t NodesPerBlock = NodeAlloc_nodes_per_block + , std::size_t Version = 2> +class node_allocator; + +#else + +//! Default options for tree-based associative containers +//! - tree_type +//! - optimize_size +typedef implementation_defined tree_assoc_defaults; + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! Type used to tag that the input range is +//! guaranteed to be ordered +struct ordered_range_t +{}; + +//! Value used to tag that the input range is +//! guaranteed to be ordered +static const ordered_range_t ordered_range = ordered_range_t(); + +//! Type used to tag that the input range is +//! guaranteed to be ordered and unique +struct ordered_unique_range_t + : public ordered_range_t +{}; + +//! Value used to tag that the input range is +//! guaranteed to be ordered and unique +static const ordered_unique_range_t ordered_unique_range = ordered_unique_range_t(); + +//! Type used to tag that the inserted values +//! should be default initialized +struct default_init_t +{}; + +//! Value used to tag that the inserted values +//! should be default initialized +static const default_init_t default_init = default_init_t(); +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! Type used to tag that the inserted values +//! should be value initialized +struct value_init_t +{}; + +//! Value used to tag that the inserted values +//! should be value initialized +static const value_init_t value_init = value_init_t(); + +namespace container_detail_really_deep_namespace { + +//Otherwise, gcc issues a warning of previously defined +//anonymous_instance and unique_instance +struct dummy +{ + dummy() + { + (void)ordered_range; + (void)ordered_unique_range; + (void)default_init; + } +}; + +} //detail_really_deep_namespace { + + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} //namespace boost { namespace container { + +#endif //#ifndef BOOST_CONTAINER_CONTAINER_FWD_HPP diff --git a/boost/container/deque.hpp b/boost/container/deque.hpp new file mode 100644 index 0000000..d8a546c --- /dev/null +++ b/boost/container/deque.hpp @@ -0,0 +1,2069 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DEQUE_HPP +#define BOOST_CONTAINER_DEQUE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +template +class deque; + +template +struct deque_value_traits +{ + typedef T value_type; + static const bool trivial_dctr = boost::has_trivial_destructor::value; + static const bool trivial_dctr_after_move = ::boost::has_trivial_destructor_after_move::value; + static const bool trivial_copy = has_trivial_copy::value; + static const bool nothrow_copy = has_nothrow_copy::value; + static const bool trivial_assign = has_trivial_assign::value; + //static const bool nothrow_assign = has_nothrow_assign::value; + static const bool nothrow_assign = false; +}; + +// Note: this function is simply a kludge to work around several compilers' +// bugs in handling constant expressions. +template +struct deque_buf_size +{ + static const std::size_t min_size = 512u; + static const std::size_t sizeof_t = sizeof(T); + static const std::size_t value = sizeof_t < min_size ? (min_size/sizeof_t) : std::size_t(1); +}; + +namespace container_detail { + +// Class invariants: +// For any nonsingular iterator i: +// i.node is the address of an element in the map array. The +// contents of i.node is a pointer to the beginning of a node. +// i.first == //(i.node) +// i.last == i.first + node_size +// i.cur is a pointer in the range [i.first, i.last). NOTE: +// the implication of this is that i.cur is always a dereferenceable +// pointer, even if i is a past-the-end iterator. +// Start and Finish are always nonsingular iterators. NOTE: this means +// that an empty deque must have one node, and that a deque +// with N elements, where N is the buffer size, must have two nodes. +// For every node other than start.node and finish.node, every element +// in the node is an initialized object. If start.node == finish.node, +// then [start.cur, finish.cur) are initialized objects, and +// the elements outside that range are uninitialized storage. Otherwise, +// [start.cur, start.last) and [finish.first, finish.cur) are initialized +// objects, and [start.first, start.cur) and [finish.cur, finish.last) +// are uninitialized storage. +// [map, map + map_size) is a valid, non-empty range. +// [start.node, finish.node] is a valid range contained within +// [map, map + map_size). +// Allocator pointer in the range [map, map + map_size) points to an allocated node +// if and only if the pointer is in the range [start.node, finish.node]. +template +class deque_iterator +{ + public: + typedef std::random_access_iterator_tag iterator_category; + typedef typename boost::intrusive::pointer_traits::element_type value_type; + typedef typename boost::intrusive::pointer_traits::difference_type difference_type; + typedef typename if_c + < IsConst + , typename boost::intrusive::pointer_traits::template + rebind_pointer::type + , Pointer + >::type pointer; + typedef typename if_c + < IsConst + , const value_type& + , value_type& + >::type reference; + + static std::size_t s_buffer_size() + { return deque_buf_size::value; } + + typedef Pointer val_alloc_ptr; + typedef typename boost::intrusive::pointer_traits:: + template rebind_pointer::type index_pointer; + + Pointer m_cur; + Pointer m_first; + Pointer m_last; + index_pointer m_node; + + public: + + Pointer get_cur() const { return m_cur; } + Pointer get_first() const { return m_first; } + Pointer get_last() const { return m_last; } + index_pointer get_node() const { return m_node; } + + deque_iterator(val_alloc_ptr x, index_pointer y) BOOST_CONTAINER_NOEXCEPT + : m_cur(x), m_first(*y), m_last(*y + s_buffer_size()), m_node(y) + {} + + deque_iterator() BOOST_CONTAINER_NOEXCEPT + : m_cur(), m_first(), m_last(), m_node() //Value initialization to achieve "null iterators" (N3644) + {} + + deque_iterator(deque_iterator const& x) BOOST_CONTAINER_NOEXCEPT + : m_cur(x.get_cur()), m_first(x.get_first()), m_last(x.get_last()), m_node(x.get_node()) + {} + + deque_iterator(Pointer cur, Pointer first, Pointer last, index_pointer node) BOOST_CONTAINER_NOEXCEPT + : m_cur(cur), m_first(first), m_last(last), m_node(node) + {} + + deque_iterator unconst() const BOOST_CONTAINER_NOEXCEPT + { + return deque_iterator(this->get_cur(), this->get_first(), this->get_last(), this->get_node()); + } + + reference operator*() const BOOST_CONTAINER_NOEXCEPT + { return *this->m_cur; } + + pointer operator->() const BOOST_CONTAINER_NOEXCEPT + { return this->m_cur; } + + difference_type operator-(const deque_iterator& x) const BOOST_CONTAINER_NOEXCEPT + { + if(!this->m_cur && !x.m_cur){ + return 0; + } + return difference_type(this->s_buffer_size()) * (this->m_node - x.m_node - 1) + + (this->m_cur - this->m_first) + (x.m_last - x.m_cur); + } + + deque_iterator& operator++() BOOST_CONTAINER_NOEXCEPT + { + ++this->m_cur; + if (this->m_cur == this->m_last) { + this->priv_set_node(this->m_node + 1); + this->m_cur = this->m_first; + } + return *this; + } + + deque_iterator operator++(int) BOOST_CONTAINER_NOEXCEPT + { + deque_iterator tmp(*this); + ++*this; + return tmp; + } + + deque_iterator& operator--() BOOST_CONTAINER_NOEXCEPT + { + if (this->m_cur == this->m_first) { + this->priv_set_node(this->m_node - 1); + this->m_cur = this->m_last; + } + --this->m_cur; + return *this; + } + + deque_iterator operator--(int) BOOST_CONTAINER_NOEXCEPT + { + deque_iterator tmp(*this); + --*this; + return tmp; + } + + deque_iterator& operator+=(difference_type n) BOOST_CONTAINER_NOEXCEPT + { + difference_type offset = n + (this->m_cur - this->m_first); + if (offset >= 0 && offset < difference_type(this->s_buffer_size())) + this->m_cur += n; + else { + difference_type node_offset = + offset > 0 ? offset / difference_type(this->s_buffer_size()) + : -difference_type((-offset - 1) / this->s_buffer_size()) - 1; + this->priv_set_node(this->m_node + node_offset); + this->m_cur = this->m_first + + (offset - node_offset * difference_type(this->s_buffer_size())); + } + return *this; + } + + deque_iterator operator+(difference_type n) const BOOST_CONTAINER_NOEXCEPT + { deque_iterator tmp(*this); return tmp += n; } + + deque_iterator& operator-=(difference_type n) BOOST_CONTAINER_NOEXCEPT + { return *this += -n; } + + deque_iterator operator-(difference_type n) const BOOST_CONTAINER_NOEXCEPT + { deque_iterator tmp(*this); return tmp -= n; } + + reference operator[](difference_type n) const BOOST_CONTAINER_NOEXCEPT + { return *(*this + n); } + + friend bool operator==(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_cur == r.m_cur; } + + friend bool operator!=(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_cur != r.m_cur; } + + friend bool operator<(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return (l.m_node == r.m_node) ? (l.m_cur < r.m_cur) : (l.m_node < r.m_node); } + + friend bool operator>(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return r < l; } + + friend bool operator<=(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return !(r < l); } + + friend bool operator>=(const deque_iterator& l, const deque_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return !(l < r); } + + void priv_set_node(index_pointer new_node) BOOST_CONTAINER_NOEXCEPT + { + this->m_node = new_node; + this->m_first = *new_node; + this->m_last = this->m_first + this->s_buffer_size(); + } + + friend deque_iterator operator+(difference_type n, deque_iterator x) BOOST_CONTAINER_NOEXCEPT + { return x += n; } +}; + +} //namespace container_detail { + +// Deque base class. It has two purposes. First, its constructor +// and destructor allocate (but don't initialize) storage. This makes +// exception safety easier. +template +class deque_base +{ + BOOST_COPYABLE_AND_MOVABLE(deque_base) + public: + typedef allocator_traits val_alloc_traits_type; + typedef typename val_alloc_traits_type::value_type val_alloc_val; + typedef typename val_alloc_traits_type::pointer val_alloc_ptr; + typedef typename val_alloc_traits_type::const_pointer val_alloc_cptr; + typedef typename val_alloc_traits_type::reference val_alloc_ref; + typedef typename val_alloc_traits_type::const_reference val_alloc_cref; + typedef typename val_alloc_traits_type::difference_type val_alloc_diff; + typedef typename val_alloc_traits_type::size_type val_alloc_size; + typedef typename val_alloc_traits_type::template + portable_rebind_alloc::type ptr_alloc_t; + typedef allocator_traits ptr_alloc_traits_type; + typedef typename ptr_alloc_traits_type::value_type ptr_alloc_val; + typedef typename ptr_alloc_traits_type::pointer ptr_alloc_ptr; + typedef typename ptr_alloc_traits_type::const_pointer ptr_alloc_cptr; + typedef typename ptr_alloc_traits_type::reference ptr_alloc_ref; + typedef typename ptr_alloc_traits_type::const_reference ptr_alloc_cref; + typedef Allocator allocator_type; + typedef allocator_type stored_allocator_type; + typedef val_alloc_size size_type; + + protected: + + typedef deque_value_traits traits_t; + typedef ptr_alloc_t map_allocator_type; + + static size_type s_buffer_size() BOOST_CONTAINER_NOEXCEPT + { return deque_buf_size::value; } + + val_alloc_ptr priv_allocate_node() + { return this->alloc().allocate(s_buffer_size()); } + + void priv_deallocate_node(val_alloc_ptr p) BOOST_CONTAINER_NOEXCEPT + { this->alloc().deallocate(p, s_buffer_size()); } + + ptr_alloc_ptr priv_allocate_map(size_type n) + { return this->ptr_alloc().allocate(n); } + + void priv_deallocate_map(ptr_alloc_ptr p, size_type n) BOOST_CONTAINER_NOEXCEPT + { this->ptr_alloc().deallocate(p, n); } + + typedef container_detail::deque_iterator iterator; + typedef container_detail::deque_iterator const_iterator; + + deque_base(size_type num_elements, const allocator_type& a) + : members_(a) + { this->priv_initialize_map(num_elements); } + + explicit deque_base(const allocator_type& a) + : members_(a) + {} + + deque_base() + : members_() + {} + + explicit deque_base(BOOST_RV_REF(deque_base) x) + : members_( boost::move(x.ptr_alloc()) + , boost::move(x.alloc()) ) + {} + + ~deque_base() + { + if (this->members_.m_map) { + this->priv_destroy_nodes(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1); + this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size); + } + } + + private: + deque_base(const deque_base&); + + protected: + + void swap_members(deque_base &x) BOOST_CONTAINER_NOEXCEPT + { + std::swap(this->members_.m_start, x.members_.m_start); + std::swap(this->members_.m_finish, x.members_.m_finish); + std::swap(this->members_.m_map, x.members_.m_map); + std::swap(this->members_.m_map_size, x.members_.m_map_size); + } + + void priv_initialize_map(size_type num_elements) + { +// if(num_elements){ + size_type num_nodes = num_elements / s_buffer_size() + 1; + + this->members_.m_map_size = container_detail::max_value((size_type) InitialMapSize, num_nodes + 2); + this->members_.m_map = this->priv_allocate_map(this->members_.m_map_size); + + ptr_alloc_ptr nstart = this->members_.m_map + (this->members_.m_map_size - num_nodes) / 2; + ptr_alloc_ptr nfinish = nstart + num_nodes; + + BOOST_TRY { + this->priv_create_nodes(nstart, nfinish); + } + BOOST_CATCH(...){ + this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size); + this->members_.m_map = 0; + this->members_.m_map_size = 0; + BOOST_RETHROW + } + BOOST_CATCH_END + + this->members_.m_start.priv_set_node(nstart); + this->members_.m_finish.priv_set_node(nfinish - 1); + this->members_.m_start.m_cur = this->members_.m_start.m_first; + this->members_.m_finish.m_cur = this->members_.m_finish.m_first + + num_elements % s_buffer_size(); +// } + } + + void priv_create_nodes(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish) + { + ptr_alloc_ptr cur; + BOOST_TRY { + for (cur = nstart; cur < nfinish; ++cur) + *cur = this->priv_allocate_node(); + } + BOOST_CATCH(...){ + this->priv_destroy_nodes(nstart, cur); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + void priv_destroy_nodes(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish) BOOST_CONTAINER_NOEXCEPT + { + for (ptr_alloc_ptr n = nstart; n < nfinish; ++n) + this->priv_deallocate_node(*n); + } + + void priv_clear_map() BOOST_CONTAINER_NOEXCEPT + { + if (this->members_.m_map) { + this->priv_destroy_nodes(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1); + this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size); + this->members_.m_map = 0; + this->members_.m_map_size = 0; + this->members_.m_start = iterator(); + this->members_.m_finish = this->members_.m_start; + } + } + + enum { InitialMapSize = 8 }; + + protected: + struct members_holder + : public ptr_alloc_t + , public allocator_type + { + members_holder() + : map_allocator_type(), allocator_type() + , m_map(0), m_map_size(0) + , m_start(), m_finish(m_start) + {} + + explicit members_holder(const allocator_type &a) + : map_allocator_type(a), allocator_type(a) + , m_map(0), m_map_size(0) + , m_start(), m_finish(m_start) + {} + + template + members_holder(BOOST_FWD_REF(PtrAllocConvertible) pa, BOOST_FWD_REF(ValAllocConvertible) va) + : map_allocator_type(boost::forward(pa)) + , allocator_type (boost::forward(va)) + , m_map(0), m_map_size(0) + , m_start(), m_finish(m_start) + {} + + ptr_alloc_ptr m_map; + val_alloc_size m_map_size; + iterator m_start; + iterator m_finish; + } members_; + + ptr_alloc_t &ptr_alloc() BOOST_CONTAINER_NOEXCEPT + { return members_; } + + const ptr_alloc_t &ptr_alloc() const BOOST_CONTAINER_NOEXCEPT + { return members_; } + + allocator_type &alloc() BOOST_CONTAINER_NOEXCEPT + { return members_; } + + const allocator_type &alloc() const BOOST_CONTAINER_NOEXCEPT + { return members_; } +}; +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +//! A double-ended queue is a sequence that supports random access to elements, constant time insertion +//! and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle. +//! +//! \tparam T The type of object that is stored in the deque +//! \tparam Allocator The allocator used for all internal memory management +template > +#else +template +#endif +class deque : protected deque_base +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + typedef deque_base Base; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef T value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(typename Base::iterator) iterator; + typedef BOOST_CONTAINER_IMPDEF(typename Base::const_iterator) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) const_reverse_iterator; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + private: // Internal typedefs + BOOST_COPYABLE_AND_MOVABLE(deque) + typedef typename Base::ptr_alloc_ptr index_pointer; + static size_type s_buffer_size() + { return Base::s_buffer_size(); } + typedef allocator_traits allocator_traits_type; + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructors a deque. + //! + //! Throws: If allocator_type's default constructor throws. + //! + //! Complexity: Constant. + deque() + : Base() + {} + + //! Effects: Constructs a deque taking the allocator as parameter. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + explicit deque(const allocator_type& a) BOOST_CONTAINER_NOEXCEPT + : Base(a) + {} + + //! Effects: Constructs a deque that will use a copy of allocator a + //! and inserts n value initialized values. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's value initialization throws. + //! + //! Complexity: Linear to n. + explicit deque(size_type n) + : Base(n, allocator_type()) + { + container_detail::insert_value_initialized_n_proxy proxy; + proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n); + //deque_base will deallocate in case of exception... + } + + //! Effects: Constructs a deque that will use a copy of allocator a + //! and inserts n default initialized values. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default initialization or copy constructor throws. + //! + //! Complexity: Linear to n. + //! + //! Note: Non-standard extension + deque(size_type n, default_init_t) + : Base(n, allocator_type()) + { + container_detail::insert_default_initialized_n_proxy proxy; + proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n); + //deque_base will deallocate in case of exception... + } + + //! Effects: Constructs a deque that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + deque(size_type n, const value_type& value, + const allocator_type& a = allocator_type()) + : Base(n, a) + { this->priv_fill_initialize(value); } + + //! Effects: Constructs a deque that will use a copy of allocator a + //! and inserts a copy of the range [first, last) in the deque. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + deque(InIt first, InIt last, const allocator_type& a = allocator_type() + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + >::type * = 0 + #endif + ) + : Base(a) + { + typedef typename std::iterator_traits::iterator_category ItCat; + this->priv_range_initialize(first, last, ItCat()); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a deque that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.end()) in the deque. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced std::initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + deque(std::initializer_list il, const allocator_type& a = allocator_type()) + : Base(a) + { + this->priv_range_initialize(il.begin(), il.end(), std::input_iterator_tag()); + } +#endif + + //! Effects: Copy constructs a deque. + //! + //! Postcondition: x == *this. + //! + //! Complexity: Linear to the elements x contains. + deque(const deque& x) + : Base(allocator_traits_type::select_on_container_copy_construction(x.alloc())) + { + if(x.size()){ + this->priv_initialize_map(x.size()); + boost::container::uninitialized_copy_alloc + (this->alloc(), x.begin(), x.end(), this->members_.m_start); + } + } + + //! Effects: Move constructor. Moves mx's resources to *this. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant. + deque(BOOST_RV_REF(deque) x) + : Base(boost::move(static_cast(x))) + { this->swap_members(x); } + + //! Effects: Copy constructs a vector using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocation + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to the elements x contains. + deque(const deque& x, const allocator_type &a) + : Base(a) + { + if(x.size()){ + this->priv_initialize_map(x.size()); + boost::container::uninitialized_copy_alloc + (this->alloc(), x.begin(), x.end(), this->members_.m_start); + } + } + + //! Effects: Move constructor using the specified allocator. + //! Moves mx's resources to *this if a == allocator_type(). + //! Otherwise copies values from x to *this. + //! + //! Throws: If allocation or T's copy constructor throws. + //! + //! Complexity: Constant if a == mx.get_allocator(), linear otherwise. + deque(BOOST_RV_REF(deque) mx, const allocator_type &a) + : Base(a) + { + if(mx.alloc() == a){ + this->swap_members(mx); + } + else{ + if(mx.size()){ + this->priv_initialize_map(mx.size()); + boost::container::uninitialized_copy_alloc + (this->alloc(), mx.begin(), mx.end(), this->members_.m_start); + } + } + } + + //! Effects: Destroys the deque. All stored values are destroyed + //! and used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements. + ~deque() BOOST_CONTAINER_NOEXCEPT + { + this->priv_destroy_range(this->members_.m_start, this->members_.m_finish); + } + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in x. + deque& operator= (BOOST_COPY_ASSIGN_REF(deque) x) + { + if (&x != this){ + allocator_type &this_alloc = this->alloc(); + const allocator_type &x_alloc = x.alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + this->shrink_to_fit(); + } + container_detail::assign_alloc(this->alloc(), x.alloc(), flag); + container_detail::assign_alloc(this->ptr_alloc(), x.ptr_alloc(), flag); + this->assign(x.cbegin(), x.cend()); + } + return *this; + } + + //! Effects: Move assignment. All x's values are transferred to *this. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + deque& operator= (BOOST_RV_REF(deque) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + BOOST_ASSERT(this != &x); + allocator_type &this_alloc = this->alloc(); + allocator_type &x_alloc = x.alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + container_detail::bool_ flag; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy objects but retain memory in case x reuses it in the future + this->clear(); + //Move allocator if needed + container_detail::move_alloc(this_alloc, x_alloc, flag); + container_detail::move_alloc(this->ptr_alloc(), x.ptr_alloc(), flag); + //Nothrow swap + this->swap_members(x); + } + //Else do a one by one move + else{ + this->assign( boost::make_move_iterator(x.begin()) + , boost::make_move_iterator(x.end())); + } + return *this; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Makes *this contain the same elements as il. + //! + //! Postcondition: this->size() == il.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in il. + deque& operator=(std::initializer_list il) + { + this->assign(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Assigns the n copies of val to *this. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + void assign(size_type n, const T& val) + { + typedef constant_iterator c_it; + this->assign(c_it(val, n), c_it()); + } + + //! Effects: Assigns the the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing InIt throws. + //! + //! Complexity: Linear to n. + template + void assign(InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + iterator cur = this->begin(); + for ( ; first != last && cur != end(); ++cur, ++first){ + *cur = *first; + } + if (first == last){ + this->erase(cur, this->cend()); + } + else{ + this->insert(this->cend(), first, last); + } + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + void assign(FwdIt first, FwdIt last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + ) + { + const size_type len = std::distance(first, last); + if (len > size()) { + FwdIt mid = first; + std::advance(mid, this->size()); + boost::container::copy(first, mid, begin()); + this->insert(this->cend(), mid, last); + } + else{ + this->erase(boost::container::copy(first, last, this->begin()), cend()); + } + } + #endif + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing std::initializer_list iterator throws. + //! + //! Complexity: Linear to il.size(). + void assign(std::initializer_list il) + { this->assign(il.begin(), il.end()); } +#endif + + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return Base::alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return Base::alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return Base::alloc(); } + + //! Effects: Returns an iterator to the first element contained in the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_start; } + + //! Effects: Returns a const_iterator to the first element contained in the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_start; } + + //! Effects: Returns an iterator to the end of the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_finish; } + + //! Effects: Returns a const_iterator to the end of the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_finish; } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->members_.m_finish); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->members_.m_finish); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->members_.m_start); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->members_.m_start); } + + //! Effects: Returns a const_iterator to the first element contained in the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_start; } + + //! Effects: Returns a const_iterator to the end of the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_finish; } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->members_.m_finish); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->members_.m_start); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the deque contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_finish == this->members_.m_start; } + + //! Effects: Returns the number of the elements contained in the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_finish - this->members_.m_start; } + + //! Effects: Returns the largest possible size of the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return allocator_traits_type::max_size(this->alloc()); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws, or T's constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size) + { + const size_type len = size(); + if (new_size < len) + this->priv_erase_last_n(len - new_size); + else{ + const size_type n = new_size - this->size(); + container_detail::insert_value_initialized_n_proxy proxy; + priv_insert_back_aux_impl(n, proxy); + } + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are default initialized. + //! + //! Throws: If memory allocation throws, or T's constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + //! + //! Note: Non-standard extension + void resize(size_type new_size, default_init_t) + { + const size_type len = size(); + if (new_size < len) + this->priv_erase_last_n(len - new_size); + else{ + const size_type n = new_size - this->size(); + container_detail::insert_default_initialized_n_proxy proxy; + priv_insert_back_aux_impl(n, proxy); + } + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size, const value_type& x) + { + const size_type len = size(); + if (new_size < len) + this->erase(this->members_.m_start + new_size, this->members_.m_finish); + else + this->insert(this->members_.m_finish, new_size - len, x); + } + + //! Effects: Tries to deallocate the excess of memory created + //! with previous allocations. The size of the deque is unchanged + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Constant. + void shrink_to_fit() + { + //This deque implementation already + //deallocates excess nodes when erasing + //so there is nothing to do except for + //empty deque + if(this->empty()){ + this->priv_clear_map(); + } + } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() BOOST_CONTAINER_NOEXCEPT + { return *this->members_.m_start; } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const BOOST_CONTAINER_NOEXCEPT + { return *this->members_.m_start; } + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference back() BOOST_CONTAINER_NOEXCEPT + { return *(end()-1); } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference back() const BOOST_CONTAINER_NOEXCEPT + { return *(cend()-1); } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_start[difference_type(n)]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT + { return this->members_.m_start[difference_type(n)]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + reference at(size_type n) + { this->priv_range_check(n); return (*this)[n]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + const_reference at(size_type n) const + { this->priv_range_check(n); return (*this)[n]; } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the beginning of the deque. + //! + //! Throws: If memory allocation throws or the in-place constructor throws. + //! + //! Complexity: Amortized constant time + template + void emplace_front(Args&&... args) + { + if(this->priv_push_front_simple_available()){ + allocator_traits_type::construct + ( this->alloc() + , this->priv_push_front_simple_pos() + , boost::forward(args)...); + this->priv_push_front_simple_commit(); + } + else{ + typedef container_detail::insert_non_movable_emplace_proxy type; + this->priv_insert_front_aux_impl(1, type(boost::forward(args)...)); + } + } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the end of the deque. + //! + //! Throws: If memory allocation throws or the in-place constructor throws. + //! + //! Complexity: Amortized constant time + template + void emplace_back(Args&&... args) + { + if(this->priv_push_back_simple_available()){ + allocator_traits_type::construct + ( this->alloc() + , this->priv_push_back_simple_pos() + , boost::forward(args)...); + this->priv_push_back_simple_commit(); + } + else{ + typedef container_detail::insert_non_movable_emplace_proxy type; + this->priv_insert_back_aux_impl(1, type(boost::forward(args)...)); + } + } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... before p + //! + //! Throws: If memory allocation throws or the in-place constructor throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + template + iterator emplace(const_iterator p, Args&&... args) + { + if(p == this->cbegin()){ + this->emplace_front(boost::forward(args)...); + return this->begin(); + } + else if(p == this->cend()){ + this->emplace_back(boost::forward(args)...); + return (this->end()-1); + } + else{ + typedef container_detail::insert_emplace_proxy type; + return this->priv_insert_aux_impl(p, 1, type(boost::forward(args)...)); + } + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + //advanced_insert_int.hpp includes all necessary preprocessor machinery... + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, > ) \ + void emplace_front(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + if(priv_push_front_simple_available()){ \ + allocator_traits_type::construct \ + ( this->alloc() \ + , this->priv_push_front_simple_pos() \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + priv_push_front_simple_commit(); \ + } \ + else{ \ + typedef container_detail::BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, n) \ + type; \ + priv_insert_front_aux_impl \ + (1, type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + } \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + if(priv_push_back_simple_available()){ \ + allocator_traits_type::construct \ + ( this->alloc() \ + , this->priv_push_back_simple_pos() \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + priv_push_back_simple_commit(); \ + } \ + else{ \ + typedef container_detail::BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, n) \ + type; \ + priv_insert_back_aux_impl \ + (1, type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + } \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + if(p == this->cbegin()){ \ + this->emplace_front(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + return this->begin(); \ + } \ + else if(p == cend()){ \ + this->emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + return (this->end()-1); \ + } \ + else{ \ + typedef container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \ + type; \ + return this->priv_insert_aux_impl \ + (p, 1, type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + } \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the front of the deque. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_front(const T &x); + + //! Effects: Constructs a new element in the front of the deque + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_front(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the end of the deque. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_back(const T &x); + + //! Effects: Constructs a new element in the end of the deque + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_back(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of x before p. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws or x's copy constructor throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator p, const T &x); + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a new element before p with mx's resources. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator p, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: pos must be a valid iterator of *this. + //! + //! Effects: Insert n copies of x before pos. + //! + //! Returns: an iterator to the first inserted element or pos if n is 0. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + iterator insert(const_iterator pos, size_type n, const value_type& x) + { + typedef constant_iterator c_it; + return this->insert(pos, c_it(x, n), c_it()); + } + + //! Requires: pos must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [first, last) range before pos. + //! + //! Returns: an iterator to the first inserted element or pos if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InIt throws or T's copy constructor throws. + //! + //! Complexity: Linear to std::distance [first, last). + template + iterator insert(const_iterator pos, InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + size_type n = 0; + iterator it(pos.unconst()); + for(;first != last; ++first, ++n){ + it = this->emplace(it, *first); + ++it; + } + it -= n; + return it; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: pos must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before pos. + //! + //! Returns: an iterator to the first inserted element or pos if il.begin() == il.end(). + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced std::initializer_list throws or T's copy constructor throws. + //! + //! Complexity: Linear to std::distance [il.begin(), il.end()). + iterator insert(const_iterator pos, std::initializer_list il) + { return insert(pos, il.begin(), il.end()); } +#endif + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator p, FwdIt first, FwdIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + container_detail::insert_range_proxy proxy(first); + return priv_insert_aux_impl(p, (size_type)std::distance(first, last), proxy); + } + #endif + + //! Effects: Removes the first element from the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + void pop_front() BOOST_CONTAINER_NOEXCEPT + { + if (this->members_.m_start.m_cur != this->members_.m_start.m_last - 1) { + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(this->members_.m_start.m_cur) + ); + ++this->members_.m_start.m_cur; + } + else + this->priv_pop_front_aux(); + } + + //! Effects: Removes the last element from the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + void pop_back() BOOST_CONTAINER_NOEXCEPT + { + if (this->members_.m_finish.m_cur != this->members_.m_finish.m_first) { + --this->members_.m_finish.m_cur; + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(this->members_.m_finish.m_cur) + ); + } + else + this->priv_pop_back_aux(); + } + + //! Effects: Erases the element at p. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the elements between pos and the + //! last element (if pos is near the end) or the first element + //! if(pos is near the beginning). + //! Constant if pos is the first or the last element. + iterator erase(const_iterator pos) BOOST_CONTAINER_NOEXCEPT + { + iterator next = pos.unconst(); + ++next; + size_type index = pos - this->members_.m_start; + if (index < (this->size()/2)) { + boost::move_backward(this->begin(), pos.unconst(), next); + pop_front(); + } + else { + boost::move(next, this->end(), pos.unconst()); + pop_back(); + } + return this->members_.m_start + index; + } + + //! Effects: Erases the elements pointed by [first, last). + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the distance between first and + //! last plus the elements between pos and the + //! last element (if pos is near the end) or the first element + //! if(pos is near the beginning). + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { + if (first == this->members_.m_start && last == this->members_.m_finish) { + this->clear(); + return this->members_.m_finish; + } + else { + const size_type n = static_cast(last - first); + const size_type elems_before = static_cast(first - this->members_.m_start); + if (elems_before < (this->size() - n) - elems_before) { + boost::move_backward(begin(), first.unconst(), last.unconst()); + iterator new_start = this->members_.m_start + n; + if(!Base::traits_t::trivial_dctr_after_move) + this->priv_destroy_range(this->members_.m_start, new_start); + this->priv_destroy_nodes(this->members_.m_start.m_node, new_start.m_node); + this->members_.m_start = new_start; + } + else { + boost::move(last.unconst(), end(), first.unconst()); + iterator new_finish = this->members_.m_finish - n; + if(!Base::traits_t::trivial_dctr_after_move) + this->priv_destroy_range(new_finish, this->members_.m_finish); + this->priv_destroy_nodes(new_finish.m_node + 1, this->members_.m_finish.m_node + 1); + this->members_.m_finish = new_finish; + } + return this->members_.m_start + elems_before; + } + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(deque &x) + { + this->swap_members(x); + container_detail::bool_ flag; + container_detail::swap_alloc(this->alloc(), x.alloc(), flag); + container_detail::swap_alloc(this->ptr_alloc(), x.ptr_alloc(), flag); + } + + //! Effects: Erases all the elements of the deque. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the deque. + void clear() BOOST_CONTAINER_NOEXCEPT + { + for (index_pointer node = this->members_.m_start.m_node + 1; + node < this->members_.m_finish.m_node; + ++node) { + this->priv_destroy_range(*node, *node + this->s_buffer_size()); + this->priv_deallocate_node(*node); + } + + if (this->members_.m_start.m_node != this->members_.m_finish.m_node) { + this->priv_destroy_range(this->members_.m_start.m_cur, this->members_.m_start.m_last); + this->priv_destroy_range(this->members_.m_finish.m_first, this->members_.m_finish.m_cur); + this->priv_deallocate_node(this->members_.m_finish.m_first); + } + else + this->priv_destroy_range(this->members_.m_start.m_cur, this->members_.m_finish.m_cur); + + this->members_.m_finish = this->members_.m_start; + } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const deque& x, const deque& y) + { return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const deque& x, const deque& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const deque& x, const deque& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const deque& x, const deque& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const deque& x, const deque& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const deque& x, const deque& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(deque& x, deque& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + + void priv_erase_last_n(size_type n) + { + if(n == this->size()) { + this->clear(); + } + else { + iterator new_finish = this->members_.m_finish - n; + if(!Base::traits_t::trivial_dctr_after_move) + this->priv_destroy_range(new_finish, this->members_.m_finish); + this->priv_destroy_nodes(new_finish.m_node + 1, this->members_.m_finish.m_node + 1); + this->members_.m_finish = new_finish; + } + } + + void priv_range_check(size_type n) const + { if (n >= this->size()) throw_out_of_range("deque::at out of range"); } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(U) x) + { + if (p == cbegin()){ + this->push_front(::boost::forward(x)); + return begin(); + } + else if (p == cend()){ + this->push_back(::boost::forward(x)); + return --end(); + } + else { + return priv_insert_aux_impl + ( p, (size_type)1 + , container_detail::get_insert_value_proxy(::boost::forward(x))); + } + } + + template + void priv_push_front(BOOST_FWD_REF(U) x) + { + if(this->priv_push_front_simple_available()){ + allocator_traits_type::construct + ( this->alloc(), this->priv_push_front_simple_pos(), ::boost::forward(x)); + this->priv_push_front_simple_commit(); + } + else{ + priv_insert_aux_impl + ( this->cbegin(), (size_type)1 + , container_detail::get_insert_value_proxy(::boost::forward(x))); + } + } + + template + void priv_push_back(BOOST_FWD_REF(U) x) + { + if(this->priv_push_back_simple_available()){ + allocator_traits_type::construct + ( this->alloc(), this->priv_push_back_simple_pos(), ::boost::forward(x)); + this->priv_push_back_simple_commit(); + } + else{ + priv_insert_aux_impl + ( this->cend(), (size_type)1 + , container_detail::get_insert_value_proxy(::boost::forward(x))); + } + } + + bool priv_push_back_simple_available() const + { + return this->members_.m_map && + (this->members_.m_finish.m_cur != (this->members_.m_finish.m_last - 1)); + } + + T *priv_push_back_simple_pos() const + { + return container_detail::to_raw_pointer(this->members_.m_finish.m_cur); + } + + void priv_push_back_simple_commit() + { + ++this->members_.m_finish.m_cur; + } + + bool priv_push_front_simple_available() const + { + return this->members_.m_map && + (this->members_.m_start.m_cur != this->members_.m_start.m_first); + } + + T *priv_push_front_simple_pos() const + { return container_detail::to_raw_pointer(this->members_.m_start.m_cur) - 1; } + + void priv_push_front_simple_commit() + { --this->members_.m_start.m_cur; } + + void priv_destroy_range(iterator p, iterator p2) + { + for(;p != p2; ++p){ + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(container_detail::iterator_to_pointer(p)) + ); + } + } + + void priv_destroy_range(pointer p, pointer p2) + { + for(;p != p2; ++p){ + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(container_detail::iterator_to_pointer(p)) + ); + } + } + + template + iterator priv_insert_aux_impl(const_iterator p, size_type n, InsertProxy proxy) + { + iterator pos(p.unconst()); + const size_type pos_n = p - this->cbegin(); + if(!this->members_.m_map){ + this->priv_initialize_map(0); + pos = this->begin(); + } + + const size_type elemsbefore = static_cast(pos - this->members_.m_start); + const size_type length = this->size(); + if (elemsbefore < length / 2) { + const iterator new_start = this->priv_reserve_elements_at_front(n); + const iterator old_start = this->members_.m_start; + if(!elemsbefore){ + proxy.uninitialized_copy_n_and_update(this->alloc(), new_start, n); + this->members_.m_start = new_start; + } + else{ + pos = this->members_.m_start + elemsbefore; + if (elemsbefore >= n) { + const iterator start_n = this->members_.m_start + n; + ::boost::container::uninitialized_move_alloc + (this->alloc(), this->members_.m_start, start_n, new_start); + this->members_.m_start = new_start; + boost::move(start_n, pos, old_start); + proxy.copy_n_and_update(this->alloc(), pos - n, n); + } + else { + const size_type mid_count = n - elemsbefore; + const iterator mid_start = old_start - mid_count; + proxy.uninitialized_copy_n_and_update(this->alloc(), mid_start, mid_count); + this->members_.m_start = mid_start; + ::boost::container::uninitialized_move_alloc + (this->alloc(), old_start, pos, new_start); + this->members_.m_start = new_start; + proxy.copy_n_and_update(this->alloc(), old_start, elemsbefore); + } + } + } + else { + const iterator new_finish = this->priv_reserve_elements_at_back(n); + const iterator old_finish = this->members_.m_finish; + const size_type elemsafter = length - elemsbefore; + if(!elemsafter){ + proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, n); + this->members_.m_finish = new_finish; + } + else{ + pos = old_finish - elemsafter; + if (elemsafter >= n) { + iterator finish_n = old_finish - difference_type(n); + ::boost::container::uninitialized_move_alloc + (this->alloc(), finish_n, old_finish, old_finish); + this->members_.m_finish = new_finish; + boost::move_backward(pos, finish_n, old_finish); + proxy.copy_n_and_update(this->alloc(), pos, n); + } + else { + const size_type raw_gap = n - elemsafter; + ::boost::container::uninitialized_move_alloc + (this->alloc(), pos, old_finish, old_finish + raw_gap); + BOOST_TRY{ + proxy.copy_n_and_update(this->alloc(), pos, elemsafter); + proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, raw_gap); + } + BOOST_CATCH(...){ + this->priv_destroy_range(old_finish, old_finish + elemsafter); + BOOST_RETHROW + } + BOOST_CATCH_END + this->members_.m_finish = new_finish; + } + } + } + return this->begin() + pos_n; + } + + template + iterator priv_insert_back_aux_impl(size_type n, InsertProxy proxy) + { + if(!this->members_.m_map){ + this->priv_initialize_map(0); + } + + iterator new_finish = this->priv_reserve_elements_at_back(n); + iterator old_finish = this->members_.m_finish; + proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, n); + this->members_.m_finish = new_finish; + return iterator(this->members_.m_finish - n); + } + + template + iterator priv_insert_front_aux_impl(size_type n, InsertProxy proxy) + { + if(!this->members_.m_map){ + this->priv_initialize_map(0); + } + + iterator new_start = this->priv_reserve_elements_at_front(n); + proxy.uninitialized_copy_n_and_update(this->alloc(), new_start, n); + this->members_.m_start = new_start; + return new_start; + } + + iterator priv_fill_insert(const_iterator pos, size_type n, const value_type& x) + { + typedef constant_iterator c_it; + return this->insert(pos, c_it(x, n), c_it()); + } + + // Precondition: this->members_.m_start and this->members_.m_finish have already been initialized, + // but none of the deque's elements have yet been constructed. + void priv_fill_initialize(const value_type& value) + { + index_pointer cur; + BOOST_TRY { + for (cur = this->members_.m_start.m_node; cur < this->members_.m_finish.m_node; ++cur){ + boost::container::uninitialized_fill_alloc + (this->alloc(), *cur, *cur + this->s_buffer_size(), value); + } + boost::container::uninitialized_fill_alloc + (this->alloc(), this->members_.m_finish.m_first, this->members_.m_finish.m_cur, value); + } + BOOST_CATCH(...){ + this->priv_destroy_range(this->members_.m_start, iterator(*cur, cur)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template + void priv_range_initialize(InIt first, InIt last, std::input_iterator_tag) + { + this->priv_initialize_map(0); + BOOST_TRY { + for ( ; first != last; ++first) + this->emplace_back(*first); + } + BOOST_CATCH(...){ + this->clear(); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template + void priv_range_initialize(FwdIt first, FwdIt last, std::forward_iterator_tag) + { + size_type n = 0; + n = std::distance(first, last); + this->priv_initialize_map(n); + + index_pointer cur_node; + BOOST_TRY { + for (cur_node = this->members_.m_start.m_node; + cur_node < this->members_.m_finish.m_node; + ++cur_node) { + FwdIt mid = first; + std::advance(mid, this->s_buffer_size()); + ::boost::container::uninitialized_copy_alloc(this->alloc(), first, mid, *cur_node); + first = mid; + } + ::boost::container::uninitialized_copy_alloc(this->alloc(), first, last, this->members_.m_finish.m_first); + } + BOOST_CATCH(...){ + this->priv_destroy_range(this->members_.m_start, iterator(*cur_node, cur_node)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + // Called only if this->members_.m_finish.m_cur == this->members_.m_finish.m_first. + void priv_pop_back_aux() BOOST_CONTAINER_NOEXCEPT + { + this->priv_deallocate_node(this->members_.m_finish.m_first); + this->members_.m_finish.priv_set_node(this->members_.m_finish.m_node - 1); + this->members_.m_finish.m_cur = this->members_.m_finish.m_last - 1; + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(this->members_.m_finish.m_cur) + ); + } + + // Called only if this->members_.m_start.m_cur == this->members_.m_start.m_last - 1. Note that + // if the deque has at least one element (a precondition for this member + // function), and if this->members_.m_start.m_cur == this->members_.m_start.m_last, then the deque + // must have at least two nodes. + void priv_pop_front_aux() BOOST_CONTAINER_NOEXCEPT + { + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(this->members_.m_start.m_cur) + ); + this->priv_deallocate_node(this->members_.m_start.m_first); + this->members_.m_start.priv_set_node(this->members_.m_start.m_node + 1); + this->members_.m_start.m_cur = this->members_.m_start.m_first; + } + + iterator priv_reserve_elements_at_front(size_type n) + { + size_type vacancies = this->members_.m_start.m_cur - this->members_.m_start.m_first; + if (n > vacancies){ + size_type new_elems = n-vacancies; + size_type new_nodes = (new_elems + this->s_buffer_size() - 1) / + this->s_buffer_size(); + size_type s = (size_type)(this->members_.m_start.m_node - this->members_.m_map); + if (new_nodes > s){ + this->priv_reallocate_map(new_nodes, true); + } + size_type i = 1; + BOOST_TRY { + for (; i <= new_nodes; ++i) + *(this->members_.m_start.m_node - i) = this->priv_allocate_node(); + } + BOOST_CATCH(...) { + for (size_type j = 1; j < i; ++j) + this->priv_deallocate_node(*(this->members_.m_start.m_node - j)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + return this->members_.m_start - difference_type(n); + } + + iterator priv_reserve_elements_at_back(size_type n) + { + size_type vacancies = (this->members_.m_finish.m_last - this->members_.m_finish.m_cur) - 1; + if (n > vacancies){ + size_type new_elems = n - vacancies; + size_type new_nodes = (new_elems + this->s_buffer_size() - 1)/s_buffer_size(); + size_type s = (size_type)(this->members_.m_map_size - (this->members_.m_finish.m_node - this->members_.m_map)); + if (new_nodes + 1 > s){ + this->priv_reallocate_map(new_nodes, false); + } + size_type i; + BOOST_TRY { + for (i = 1; i <= new_nodes; ++i) + *(this->members_.m_finish.m_node + i) = this->priv_allocate_node(); + } + BOOST_CATCH(...) { + for (size_type j = 1; j < i; ++j) + this->priv_deallocate_node(*(this->members_.m_finish.m_node + j)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + return this->members_.m_finish + difference_type(n); + } + + void priv_reallocate_map(size_type nodes_to_add, bool add_at_front) + { + size_type old_num_nodes = this->members_.m_finish.m_node - this->members_.m_start.m_node + 1; + size_type new_num_nodes = old_num_nodes + nodes_to_add; + + index_pointer new_nstart; + if (this->members_.m_map_size > 2 * new_num_nodes) { + new_nstart = this->members_.m_map + (this->members_.m_map_size - new_num_nodes) / 2 + + (add_at_front ? nodes_to_add : 0); + if (new_nstart < this->members_.m_start.m_node) + boost::move(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart); + else + boost::move_backward + (this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart + old_num_nodes); + } + else { + size_type new_map_size = + this->members_.m_map_size + container_detail::max_value(this->members_.m_map_size, nodes_to_add) + 2; + + index_pointer new_map = this->priv_allocate_map(new_map_size); + new_nstart = new_map + (new_map_size - new_num_nodes) / 2 + + (add_at_front ? nodes_to_add : 0); + boost::move(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart); + this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size); + + this->members_.m_map = new_map; + this->members_.m_map_size = new_map_size; + } + + this->members_.m_start.priv_set_node(new_nstart); + this->members_.m_finish.priv_set_node(new_nstart + old_num_nodes - 1); + } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +}} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public ::boost::has_trivial_destructor_after_move +{}; + +} + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +#endif // #ifndef BOOST_CONTAINER_DEQUE_HPP diff --git a/boost/container/detail/adaptive_node_pool.hpp b/boost/container/detail/adaptive_node_pool.hpp new file mode 100644 index 0000000..4e73754 --- /dev/null +++ b/boost/container/detail/adaptive_node_pool.hpp @@ -0,0 +1,162 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_HPP +#define BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace boost { +namespace container { +namespace container_detail { + +template +struct select_private_adaptive_node_pool_impl +{ + typedef boost::container::container_detail:: + private_adaptive_node_pool_impl + < fake_segment_manager + , unsigned(AlignOnly)*::boost::container::adaptive_pool_flag::align_only + | ::boost::container::adaptive_pool_flag::size_ordered | ::boost::container::adaptive_pool_flag::address_ordered + > type; +}; + +//!Pooled memory allocator using an smart adaptive pool. Includes +//!a reference count but the class does not delete itself, this is +//!responsibility of user classes. Node size (NodeSize) and the number of +//!nodes allocated per block (NodesPerBlock) are known at compile time. +template< std::size_t NodeSize + , std::size_t NodesPerBlock + , std::size_t MaxFreeBlocks + , std::size_t OverheadPercent + > +class private_adaptive_node_pool + : public select_private_adaptive_node_pool_impl<(OverheadPercent == 0)>::type +{ + typedef typename select_private_adaptive_node_pool_impl::type base_t; + //Non-copyable + private_adaptive_node_pool(const private_adaptive_node_pool &); + private_adaptive_node_pool &operator=(const private_adaptive_node_pool &); + + public: + typedef typename base_t::multiallocation_chain multiallocation_chain; + static const std::size_t nodes_per_block = NodesPerBlock; + + //!Constructor. Never throws + private_adaptive_node_pool() + : base_t(0 + , NodeSize + , NodesPerBlock + , MaxFreeBlocks + , (unsigned char)OverheadPercent) + {} +}; + +//!Pooled memory allocator using adaptive pool. Includes +//!a reference count but the class does not delete itself, this is +//!responsibility of user classes. Node size (NodeSize) and the number of +//!nodes allocated per block (NodesPerBlock) are known at compile time +template< std::size_t NodeSize + , std::size_t NodesPerBlock + , std::size_t MaxFreeBlocks + , std::size_t OverheadPercent + > +class shared_adaptive_node_pool + : public private_adaptive_node_pool + +{ + private: + typedef private_adaptive_node_pool + private_node_allocator_t; + public: + typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain; + + //!Constructor. Never throws + shared_adaptive_node_pool() + : private_node_allocator_t(){} + + //!Destructor. Deallocates all allocated blocks. Never throws + ~shared_adaptive_node_pool() + {} + + //!Allocates array of count elements. Can throw std::bad_alloc + void *allocate_node() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + return private_node_allocator_t::allocate_node(); + } + + //!Deallocates an array pointed by ptr. Never throws + void deallocate_node(void *ptr) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_node(ptr); + } + + //!Allocates a singly linked list of n nodes ending in null pointer. + //!can throw std::bad_alloc + void allocate_nodes(const std::size_t n, multiallocation_chain &chain) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + return private_node_allocator_t::allocate_nodes(n, chain); + } + + void deallocate_nodes(multiallocation_chain &chain) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_nodes(chain); + } + + //!Deallocates all the free blocks of memory. Never throws + void deallocate_free_blocks() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_free_blocks(); + } + + private: + default_mutex mutex_; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_HPP diff --git a/boost/container/detail/adaptive_node_pool_impl.hpp b/boost/container/detail/adaptive_node_pool_impl.hpp new file mode 100644 index 0000000..dc1a7f1 --- /dev/null +++ b/boost/container/detail/adaptive_node_pool_impl.hpp @@ -0,0 +1,875 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP +#define BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +namespace adaptive_pool_flag { + +static const unsigned int none = 0u; +static const unsigned int align_only = 1u << 0u; +static const unsigned int size_ordered = 1u << 1u; +static const unsigned int address_ordered = 1u << 2u; + +} //namespace adaptive_pool_flag{ + +namespace container_detail { + +template +struct hdr_offset_holder_t +{ + hdr_offset_holder_t(size_type offset = 0) + : hdr_offset(offset) + {} + size_type hdr_offset; +}; + +template +struct less_func; + +template +struct less_func +{ + static bool less(SizeType, SizeType, const void *, const void *) + { return true; } +}; + +template +struct less_func +{ + static bool less(SizeType ls, SizeType rs, const void *, const void *) + { return ls < rs; } +}; + +template +struct less_func +{ + static bool less(SizeType, SizeType, const void *la, const void *ra) + { return &la < &ra; } +}; + +template +struct less_func +{ + static bool less(SizeType ls, SizeType rs, const void *la, const void *ra) + { return (ls < rs) || ((ls == rs) && (la < ra)); } +}; + +template +struct block_container_traits +{ + typedef typename bi::make_set_base_hook + < bi::void_pointer + , bi::optimize_size + , bi::link_mode >::type hook_t; + + template + struct container + { + typedef typename bi::make_multiset + , bi::size_type >::type type; + }; + + template + static void reinsert_was_used(Container &container, typename Container::reference v, bool) + { + typedef typename Container::const_iterator const_block_iterator; + const const_block_iterator this_block + (Container::s_iterator_to(const_cast(v))); + const_block_iterator next_block(this_block); + if(++next_block != container.cend()){ + if(this_block->free_nodes.size() > next_block->free_nodes.size()){ + container.erase(this_block); + container.insert(v); + } + } + } + + template + static void insert_was_empty(Container &container, typename Container::value_type &v, bool) + { + container.insert(v); + } + + template + static void erase_first(Container &container) + { + container.erase(container.cbegin()); + } + + template + static void erase_last(Container &container) + { + container.erase(--container.cend()); + } +}; + +template +struct block_container_traits +{ + typedef typename bi::make_list_base_hook + < bi::void_pointer + , bi::link_mode >::type hook_t; + + template + struct container + { + typedef typename bi::make_list + , bi::size_type, bi::constant_time_size >::type type; + }; + + template + static void reinsert_was_used(Container &container, typename Container::value_type &v, bool is_full) + { + if(is_full){ + container.erase(Container::s_iterator_to(v)); + container.push_back(v); + } + } + + template + static void insert_was_empty(Container &container, typename Container::value_type &v, bool is_full) + { + if(is_full){ + container.push_back(v); + } + else{ + container.push_front(v); + } + } + + template + static void erase_first(Container &container) + { + container.pop_front(); + } + + template + static void erase_last(Container &container) + { + container.pop_back(); + } +}; + +template +struct adaptive_pool_types +{ + typedef VoidPointer void_pointer; + static const bool ordered = (Flags & (adaptive_pool_flag::size_ordered | adaptive_pool_flag::address_ordered)) != 0; + typedef block_container_traits block_container_traits_t; + typedef typename block_container_traits_t::hook_t hook_t; + typedef hdr_offset_holder_t hdr_offset_holder; + static const unsigned int order_flags = Flags & (adaptive_pool_flag::size_ordered | adaptive_pool_flag::address_ordered); + typedef MultiallocationChain free_nodes_t; + + struct block_info_t + : public hdr_offset_holder, + public hook_t + { + //An intrusive list of free node from this block + free_nodes_t free_nodes; + friend bool operator <(const block_info_t &l, const block_info_t &r) + { + return less_func:: + less(l.free_nodes.size(), r.free_nodes.size(), &l , &r); + } + + friend bool operator ==(const block_info_t &l, const block_info_t &r) + { return &l == &r; } + }; + typedef typename block_container_traits_t:: template container::type block_container_t; +}; + +template +inline size_type calculate_alignment + ( size_type overhead_percent, size_type real_node_size + , size_type hdr_size, size_type hdr_offset_size, size_type payload_per_allocation) +{ + //to-do: handle real_node_size != node_size + const size_type divisor = overhead_percent*real_node_size; + const size_type dividend = hdr_offset_size*100; + size_type elements_per_subblock = (dividend - 1)/divisor + 1; + size_type candidate_power_of_2 = + upper_power_of_2(elements_per_subblock*real_node_size + hdr_offset_size); + bool overhead_satisfied = false; + //Now calculate the wors-case overhead for a subblock + const size_type max_subblock_overhead = hdr_size + payload_per_allocation; + while(!overhead_satisfied){ + elements_per_subblock = (candidate_power_of_2 - max_subblock_overhead)/real_node_size; + const size_type overhead_size = candidate_power_of_2 - elements_per_subblock*real_node_size; + if(overhead_size*100/candidate_power_of_2 < overhead_percent){ + overhead_satisfied = true; + } + else{ + candidate_power_of_2 <<= 1; + } + } + return candidate_power_of_2; +} + +template +inline void calculate_num_subblocks + (size_type alignment, size_type real_node_size, size_type elements_per_block + , size_type &num_subblocks, size_type &real_num_node, size_type overhead_percent + , size_type hdr_size, size_type hdr_offset_size, size_type payload_per_allocation) +{ + const size_type hdr_subblock_elements = (alignment - hdr_size - payload_per_allocation)/real_node_size; + size_type elements_per_subblock = (alignment - hdr_offset_size)/real_node_size; + size_type possible_num_subblock = (elements_per_block - 1)/elements_per_subblock + 1; + while(((possible_num_subblock-1)*elements_per_subblock + hdr_subblock_elements) < elements_per_block){ + ++possible_num_subblock; + } + elements_per_subblock = (alignment - hdr_offset_size)/real_node_size; + bool overhead_satisfied = false; + while(!overhead_satisfied){ + const size_type total_data = (elements_per_subblock*(possible_num_subblock-1) + hdr_subblock_elements)*real_node_size; + const size_type total_size = alignment*possible_num_subblock; + if((total_size - total_data)*100/total_size < overhead_percent){ + overhead_satisfied = true; + } + else{ + ++possible_num_subblock; + } + } + num_subblocks = possible_num_subblock; + real_num_node = (possible_num_subblock-1)*elements_per_subblock + hdr_subblock_elements; +} + +template +class private_adaptive_node_pool_impl +{ + //Non-copyable + private_adaptive_node_pool_impl(); + private_adaptive_node_pool_impl(const private_adaptive_node_pool_impl &); + private_adaptive_node_pool_impl &operator=(const private_adaptive_node_pool_impl &); + typedef private_adaptive_node_pool_impl this_type; + + typedef typename SegmentManagerBase::void_pointer void_pointer; + static const typename SegmentManagerBase:: + size_type PayloadPerAllocation = SegmentManagerBase::PayloadPerAllocation; + //Flags + //align_only + static const bool AlignOnly = (Flags & adaptive_pool_flag::align_only) != 0; + typedef bool_ IsAlignOnly; + typedef true_ AlignOnlyTrue; + typedef false_ AlignOnlyFalse; + //size_ordered + static const bool SizeOrdered = (Flags & adaptive_pool_flag::size_ordered) != 0; + typedef bool_ IsSizeOrdered; + typedef true_ SizeOrderedTrue; + typedef false_ SizeOrderedFalse; + //address_ordered + static const bool AddressOrdered = (Flags & adaptive_pool_flag::address_ordered) != 0; + typedef bool_ IsAddressOrdered; + typedef true_ AddressOrderedTrue; + typedef false_ AddressOrderedFalse; + + public: + typedef typename SegmentManagerBase::multiallocation_chain multiallocation_chain; + typedef typename SegmentManagerBase::size_type size_type; + + private: + typedef adaptive_pool_types + adaptive_pool_types_t; + typedef typename adaptive_pool_types_t::free_nodes_t free_nodes_t; + typedef typename adaptive_pool_types_t::block_info_t block_info_t; + typedef typename adaptive_pool_types_t::block_container_t block_container_t; + typedef typename adaptive_pool_types_t::block_container_traits_t block_container_traits_t; + typedef typename block_container_t::iterator block_iterator; + typedef typename block_container_t::const_iterator const_block_iterator; + typedef typename adaptive_pool_types_t::hdr_offset_holder hdr_offset_holder; + + static const size_type MaxAlign = alignment_of::value; + static const size_type HdrSize = ((sizeof(block_info_t)-1)/MaxAlign+1)*MaxAlign; + static const size_type HdrOffsetSize = ((sizeof(hdr_offset_holder)-1)/MaxAlign+1)*MaxAlign; + + public: + //!Segment manager typedef + typedef SegmentManagerBase segment_manager_base_type; + + //!Constructor from a segment manager. Never throws + private_adaptive_node_pool_impl + ( segment_manager_base_type *segment_mngr_base + , size_type node_size + , size_type nodes_per_block + , size_type max_free_blocks + , unsigned char overhead_percent + ) + : m_max_free_blocks(max_free_blocks) + , m_real_node_size(lcm(node_size, size_type(alignment_of::value))) + //Round the size to a power of two value. + //This is the total memory size (including payload) that we want to + //allocate from the general-purpose allocator + , m_real_block_alignment + (AlignOnly ? + upper_power_of_2(HdrSize + m_real_node_size*nodes_per_block) : + calculate_alignment( (size_type)overhead_percent, m_real_node_size + , HdrSize, HdrOffsetSize, PayloadPerAllocation)) + //This is the real number of nodes per block + , m_num_subblocks(0) + , m_real_num_node(AlignOnly ? (m_real_block_alignment - PayloadPerAllocation - HdrSize)/m_real_node_size : 0) + //General purpose allocator + , mp_segment_mngr_base(segment_mngr_base) + , m_block_container() + , m_totally_free_blocks(0) + { + if(!AlignOnly){ + calculate_num_subblocks + ( m_real_block_alignment + , m_real_node_size + , nodes_per_block + , m_num_subblocks + , m_real_num_node + , (size_type)overhead_percent + , HdrSize + , HdrOffsetSize + , PayloadPerAllocation); + } + } + + //!Destructor. Deallocates all allocated blocks. Never throws + ~private_adaptive_node_pool_impl() + { this->priv_clear(); } + + size_type get_real_num_node() const + { return m_real_num_node; } + + //!Returns the segment manager. Never throws + segment_manager_base_type* get_segment_manager_base()const + { return container_detail::to_raw_pointer(mp_segment_mngr_base); } + + //!Allocates array of count elements. Can throw + void *allocate_node() + { + this->priv_invariants(); + //If there are no free nodes we allocate a new block + if(!m_block_container.empty()){ + //We take the first free node the multiset can't be empty + free_nodes_t &free_nodes = m_block_container.begin()->free_nodes; + BOOST_ASSERT(!free_nodes.empty()); + const size_type free_nodes_count = free_nodes.size(); + void *first_node = container_detail::to_raw_pointer(free_nodes.pop_front()); + if(free_nodes.empty()){ + block_container_traits_t::erase_first(m_block_container); + } + m_totally_free_blocks -= static_cast(free_nodes_count == m_real_num_node); + this->priv_invariants(); + return first_node; + } + else{ + multiallocation_chain chain; + this->priv_append_from_new_blocks(1, chain, IsAlignOnly()); + return container_detail::to_raw_pointer(chain.pop_front()); + } + } + + //!Deallocates an array pointed by ptr. Never throws + void deallocate_node(void *pElem) + { + this->priv_invariants(); + block_info_t &block_info = *this->priv_block_from_node(pElem); + BOOST_ASSERT(block_info.free_nodes.size() < m_real_num_node); + + //We put the node at the beginning of the free node list + block_info.free_nodes.push_back(void_pointer(pElem)); + + //The loop reinserts all blocks except the last one + this->priv_reinsert_block(block_info, block_info.free_nodes.size() == 1); + this->priv_deallocate_free_blocks(m_max_free_blocks); + this->priv_invariants(); + } + + //!Allocates n nodes. + //!Can throw + void allocate_nodes(const size_type n, multiallocation_chain &chain) + { + size_type i = 0; + BOOST_TRY{ + this->priv_invariants(); + while(i != n){ + //If there are no free nodes we allocate all needed blocks + if (m_block_container.empty()){ + this->priv_append_from_new_blocks(n - i, chain, IsAlignOnly()); + BOOST_ASSERT(m_block_container.empty() || (++m_block_container.cbegin() == m_block_container.cend())); + BOOST_ASSERT(chain.size() == n); + break; + } + free_nodes_t &free_nodes = m_block_container.begin()->free_nodes; + const size_type free_nodes_count_before = free_nodes.size(); + m_totally_free_blocks -= static_cast(free_nodes_count_before == m_real_num_node); + const size_type num_left = n-i; + const size_type num_elems = (num_left < free_nodes_count_before) ? num_left : free_nodes_count_before; + typedef typename free_nodes_t::iterator free_nodes_iterator; + + if(num_left < free_nodes_count_before){ + const free_nodes_iterator it_bbeg(free_nodes.before_begin()); + free_nodes_iterator it_bend(it_bbeg); + for(size_type j = 0; j != num_elems; ++j){ + ++it_bend; + } + free_nodes_iterator it_end = it_bend; ++it_end; + free_nodes_iterator it_beg = it_bbeg; ++it_beg; + free_nodes.erase_after(it_bbeg, it_end, num_elems); + chain.incorporate_after(chain.last(), &*it_beg, &*it_bend, num_elems); + //chain.splice_after(chain.last(), free_nodes, it_bbeg, it_bend, num_elems); + BOOST_ASSERT(!free_nodes.empty()); + } + else{ + const free_nodes_iterator it_beg(free_nodes.begin()), it_bend(free_nodes.last()); + free_nodes.clear(); + chain.incorporate_after(chain.last(), &*it_beg, &*it_bend, num_elems); + block_container_traits_t::erase_first(m_block_container); + } + i += num_elems; + } + } + BOOST_CATCH(...){ + this->deallocate_nodes(chain); + BOOST_RETHROW + } + BOOST_CATCH_END + this->priv_invariants(); + } + + //!Deallocates a linked list of nodes. Never throws + void deallocate_nodes(multiallocation_chain &nodes) + { + this->priv_invariants(); + //To take advantage of node locality, wait until two + //nodes belong to different blocks. Only then reinsert + //the block of the first node in the block tree. + //Cache of the previous block + block_info_t *prev_block_info = 0; + + //If block was empty before this call, it's not already + //inserted in the block tree. + bool prev_block_was_empty = false; + typedef typename free_nodes_t::iterator free_nodes_iterator; + { + const free_nodes_iterator itbb(nodes.before_begin()), ite(nodes.end()); + free_nodes_iterator itf(nodes.begin()), itbf(itbb); + size_type splice_node_count = size_type(-1); + while(itf != ite){ + void *pElem = container_detail::to_raw_pointer(container_detail::iterator_to_pointer(itf)); + block_info_t &block_info = *this->priv_block_from_node(pElem); + BOOST_ASSERT(block_info.free_nodes.size() < m_real_num_node); + ++splice_node_count; + + //If block change is detected calculate the cached block position in the tree + if(&block_info != prev_block_info){ + if(prev_block_info){ //Make sure we skip the initial "dummy" cache + free_nodes_iterator it(itbb); ++it; + nodes.erase_after(itbb, itf, splice_node_count); + prev_block_info->free_nodes.incorporate_after(prev_block_info->free_nodes.last(), &*it, &*itbf, splice_node_count); + this->priv_reinsert_block(*prev_block_info, prev_block_was_empty); + splice_node_count = 0; + } + //Update cache with new data + prev_block_was_empty = block_info.free_nodes.empty(); + prev_block_info = &block_info; + } + itbf = itf; + ++itf; + } + } + if(prev_block_info){ + //The loop reinserts all blocks except the last one + const free_nodes_iterator itfirst(nodes.begin()), itlast(nodes.last()); + const size_type splice_node_count = nodes.size(); + nodes.clear(); + prev_block_info->free_nodes.incorporate_after(prev_block_info->free_nodes.last(), &*itfirst, &*itlast, splice_node_count); + this->priv_reinsert_block(*prev_block_info, prev_block_was_empty); + this->priv_invariants(); + this->priv_deallocate_free_blocks(m_max_free_blocks); + } + } + + void deallocate_free_blocks() + { this->priv_deallocate_free_blocks(0); } + + size_type num_free_nodes() + { + typedef typename block_container_t::const_iterator citerator; + size_type count = 0; + citerator it (m_block_container.begin()), itend(m_block_container.end()); + for(; it != itend; ++it){ + count += it->free_nodes.size(); + } + return count; + } + + void swap(private_adaptive_node_pool_impl &other) + { + BOOST_ASSERT(m_max_free_blocks == other.m_max_free_blocks); + BOOST_ASSERT(m_real_node_size == other.m_real_node_size); + BOOST_ASSERT(m_real_block_alignment == other.m_real_block_alignment); + BOOST_ASSERT(m_real_num_node == other.m_real_num_node); + std::swap(mp_segment_mngr_base, other.mp_segment_mngr_base); + std::swap(m_totally_free_blocks, other.m_totally_free_blocks); + m_block_container.swap(other.m_block_container); + } + + //Deprecated, use deallocate_free_blocks + void deallocate_free_chunks() + { this->priv_deallocate_free_blocks(0); } + + private: + + void priv_deallocate_free_blocks(size_type max_free_blocks) + { //Trampoline function to ease inlining + if(m_totally_free_blocks > max_free_blocks){ + this->priv_deallocate_free_blocks_impl(max_free_blocks); + } + } + + void priv_deallocate_free_blocks_impl(size_type max_free_blocks) + { + this->priv_invariants(); + //Now check if we've reached the free nodes limit + //and check if we have free blocks. If so, deallocate as much + //as we can to stay below the limit + multiallocation_chain chain; + { + const const_block_iterator itend = m_block_container.cend(); + const_block_iterator it = itend; + --it; + size_type totally_free_blocks = m_totally_free_blocks; + + for( ; totally_free_blocks > max_free_blocks; --totally_free_blocks){ + BOOST_ASSERT(it->free_nodes.size() == m_real_num_node); + void *addr = priv_first_subblock_from_block(const_cast(&*it)); + --it; + block_container_traits_t::erase_last(m_block_container); + chain.push_front(void_pointer(addr)); + } + BOOST_ASSERT((m_totally_free_blocks - max_free_blocks) == chain.size()); + m_totally_free_blocks = max_free_blocks; + } + this->mp_segment_mngr_base->deallocate_many(chain); + } + + void priv_reinsert_block(block_info_t &prev_block_info, const bool prev_block_was_empty) + { + //Cache the free nodes from the block + const size_type this_block_free_nodes = prev_block_info.free_nodes.size(); + const bool is_full = this_block_free_nodes == m_real_num_node; + + //Update free block count + m_totally_free_blocks += static_cast(is_full); + if(prev_block_was_empty){ + block_container_traits_t::insert_was_empty(m_block_container, prev_block_info, is_full); + } + else{ + block_container_traits_t::reinsert_was_used(m_block_container, prev_block_info, is_full); + } + } + + class block_destroyer; + friend class block_destroyer; + + class block_destroyer + { + public: + block_destroyer(const this_type *impl, multiallocation_chain &chain) + : mp_impl(impl), m_chain(chain) + {} + + void operator()(typename block_container_t::pointer to_deallocate) + { return this->do_destroy(to_deallocate, IsAlignOnly()); } + + private: + void do_destroy(typename block_container_t::pointer to_deallocate, AlignOnlyTrue) + { + BOOST_ASSERT(to_deallocate->free_nodes.size() == mp_impl->m_real_num_node); + m_chain.push_back(to_deallocate); + } + + void do_destroy(typename block_container_t::pointer to_deallocate, AlignOnlyFalse) + { + BOOST_ASSERT(to_deallocate->free_nodes.size() == mp_impl->m_real_num_node); + BOOST_ASSERT(0 == to_deallocate->hdr_offset); + hdr_offset_holder *hdr_off_holder = + mp_impl->priv_first_subblock_from_block(container_detail::to_raw_pointer(to_deallocate)); + m_chain.push_back(hdr_off_holder); + } + + const this_type *mp_impl; + multiallocation_chain &m_chain; + }; + + //This macro will activate invariant checking. Slow, but helpful for debugging the code. + //#define BOOST_CONTAINER_ADAPTIVE_NODE_POOL_CHECK_INVARIANTS + void priv_invariants() + #ifdef BOOST_CONTAINER_ADAPTIVE_NODE_POOL_CHECK_INVARIANTS + #undef BOOST_CONTAINER_ADAPTIVE_NODE_POOL_CHECK_INVARIANTS + { + const const_block_iterator itend(m_block_container.end()); + + { //We iterate through the block tree to free the memory + const_block_iterator it(m_block_container.begin()); + + if(it != itend){ + for(++it; it != itend; ++it){ + const_block_iterator prev(it); + --prev; + BOOST_ASSERT(*prev < *it); + (void)prev; (void)it; + } + } + } + { //Check that the total free nodes are correct + const_block_iterator it(m_block_container.cbegin()); + size_type total_free_nodes = 0; + for(; it != itend; ++it){ + total_free_nodes += it->free_nodes.size(); + } + BOOST_ASSERT(total_free_nodes >= m_totally_free_blocks*m_real_num_node); + } + { //Check that the total totally free blocks are correct + BOOST_ASSERT(m_block_container.size() >= m_totally_free_blocks); + const_block_iterator it = m_block_container.cend(); + size_type total_free_blocks = m_totally_free_blocks; + while(total_free_blocks--){ + BOOST_ASSERT((--it)->free_nodes.size() == m_real_num_node); + } + } + + if(!AlignOnly){ + //Check that header offsets are correct + const_block_iterator it = m_block_container.begin(); + for(; it != itend; ++it){ + hdr_offset_holder *hdr_off_holder = this->priv_first_subblock_from_block(const_cast(&*it)); + for(size_type i = 0, max = m_num_subblocks; i < max; ++i){ + const size_type offset = reinterpret_cast(const_cast(&*it)) - reinterpret_cast(hdr_off_holder); + BOOST_ASSERT(hdr_off_holder->hdr_offset == offset); + BOOST_ASSERT(0 == ((size_type)hdr_off_holder & (m_real_block_alignment - 1))); + BOOST_ASSERT(0 == (hdr_off_holder->hdr_offset & (m_real_block_alignment - 1))); + hdr_off_holder = reinterpret_cast(reinterpret_cast(hdr_off_holder) + m_real_block_alignment); + } + } + } + } + #else + {} //empty + #endif + + //!Deallocates all used memory. Never throws + void priv_clear() + { + #ifndef NDEBUG + block_iterator it = m_block_container.begin(); + block_iterator itend = m_block_container.end(); + size_type n_free_nodes = 0; + for(; it != itend; ++it){ + //Check for memory leak + BOOST_ASSERT(it->free_nodes.size() == m_real_num_node); + ++n_free_nodes; + } + BOOST_ASSERT(n_free_nodes == m_totally_free_blocks); + #endif + //Check for memory leaks + this->priv_invariants(); + multiallocation_chain chain; + m_block_container.clear_and_dispose(block_destroyer(this, chain)); + this->mp_segment_mngr_base->deallocate_many(chain); + m_totally_free_blocks = 0; + } + + block_info_t *priv_block_from_node(void *node, AlignOnlyFalse) const + { + hdr_offset_holder *hdr_off_holder = + reinterpret_cast((std::size_t)node & size_type(~(m_real_block_alignment - 1))); + BOOST_ASSERT(0 == ((std::size_t)hdr_off_holder & (m_real_block_alignment - 1))); + BOOST_ASSERT(0 == (hdr_off_holder->hdr_offset & (m_real_block_alignment - 1))); + block_info_t *block = reinterpret_cast + (reinterpret_cast(hdr_off_holder) + hdr_off_holder->hdr_offset); + BOOST_ASSERT(block->hdr_offset == 0); + return block; + } + + block_info_t *priv_block_from_node(void *node, AlignOnlyTrue) const + { + return (block_info_t *)((std::size_t)node & std::size_t(~(m_real_block_alignment - 1))); + } + + block_info_t *priv_block_from_node(void *node) const + { return this->priv_block_from_node(node, IsAlignOnly()); } + + hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block) const + { return this->priv_first_subblock_from_block(block, IsAlignOnly()); } + + hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block, AlignOnlyFalse) const + { + hdr_offset_holder *const hdr_off_holder = reinterpret_cast + (reinterpret_cast(block) - (m_num_subblocks-1)*m_real_block_alignment); + BOOST_ASSERT(hdr_off_holder->hdr_offset == size_type(reinterpret_cast(block) - reinterpret_cast(hdr_off_holder))); + BOOST_ASSERT(0 == ((std::size_t)hdr_off_holder & (m_real_block_alignment - 1))); + BOOST_ASSERT(0 == (hdr_off_holder->hdr_offset & (m_real_block_alignment - 1))); + return hdr_off_holder; + } + + hdr_offset_holder *priv_first_subblock_from_block(block_info_t *block, AlignOnlyTrue) const + { + return reinterpret_cast(block); + } + + void priv_dispatch_block_chain_or_free + ( multiallocation_chain &chain, block_info_t &c_info, size_type num_node + , char *mem_address, size_type total_elements, bool insert_block_if_free) + { + BOOST_ASSERT(chain.size() <= total_elements); + //First add all possible nodes to the chain + const size_type left = total_elements - chain.size(); + const size_type max_chain = (num_node < left) ? num_node : left; + mem_address = static_cast(container_detail::to_raw_pointer + (chain.incorporate_after(chain.last(), void_pointer(mem_address), m_real_node_size, max_chain))); + //Now store remaining nodes in the free list + if(const size_type max_free = num_node - max_chain){ + free_nodes_t & free_nodes = c_info.free_nodes; + free_nodes.incorporate_after(free_nodes.last(), void_pointer(mem_address), m_real_node_size, max_free); + if(insert_block_if_free){ + m_block_container.push_front(c_info); + } + } + } + + //!Allocates a several blocks of nodes. Can throw + void priv_append_from_new_blocks(size_type min_elements, multiallocation_chain &chain, AlignOnlyTrue) + { + BOOST_ASSERT(m_block_container.empty()); + BOOST_ASSERT(min_elements > 0); + const size_type n = (min_elements - 1)/m_real_num_node + 1; + const size_type real_block_size = m_real_block_alignment - PayloadPerAllocation; + const size_type total_elements = chain.size() + min_elements; + for(size_type i = 0; i != n; ++i){ + //We allocate a new NodeBlock and put it the last + //element of the tree + char *mem_address = static_cast + (mp_segment_mngr_base->allocate_aligned(real_block_size, m_real_block_alignment)); + if(!mem_address){ + //In case of error, free memory deallocating all nodes (the new ones allocated + //in this function plus previously stored nodes in chain). + this->deallocate_nodes(chain); + throw_bad_alloc(); + } + block_info_t &c_info = *new(mem_address)block_info_t(); + mem_address += HdrSize; + if(i != (n-1)){ + chain.incorporate_after(chain.last(), void_pointer(mem_address), m_real_node_size, m_real_num_node); + } + else{ + this->priv_dispatch_block_chain_or_free(chain, c_info, m_real_num_node, mem_address, total_elements, true); + } + } + } + + void priv_append_from_new_blocks(size_type min_elements, multiallocation_chain &chain, AlignOnlyFalse) + { + BOOST_ASSERT(m_block_container.empty()); + BOOST_ASSERT(min_elements > 0); + const size_type n = (min_elements - 1)/m_real_num_node + 1; + const size_type real_block_size = m_real_block_alignment*m_num_subblocks - PayloadPerAllocation; + const size_type elements_per_subblock = (m_real_block_alignment - HdrOffsetSize)/m_real_node_size; + const size_type hdr_subblock_elements = (m_real_block_alignment - HdrSize - PayloadPerAllocation)/m_real_node_size; + const size_type total_elements = chain.size() + min_elements; + + for(size_type i = 0; i != n; ++i){ + //We allocate a new NodeBlock and put it the last + //element of the tree + char *mem_address = static_cast + (mp_segment_mngr_base->allocate_aligned(real_block_size, m_real_block_alignment)); + if(!mem_address){ + //In case of error, free memory deallocating all nodes (the new ones allocated + //in this function plus previously stored nodes in chain). + this->deallocate_nodes(chain); + throw_bad_alloc(); + } + //First initialize header information on the last subblock + char *hdr_addr = mem_address + m_real_block_alignment*(m_num_subblocks-1); + block_info_t &c_info = *new(hdr_addr)block_info_t(); + //Some structural checks + BOOST_ASSERT(static_cast(&static_cast(c_info).hdr_offset) == + static_cast(&c_info)); (void)c_info; + if(i != (n-1)){ + for( size_type subblock = 0, maxsubblock = m_num_subblocks - 1 + ; subblock < maxsubblock + ; ++subblock, mem_address += m_real_block_alignment){ + //Initialize header offset mark + new(mem_address) hdr_offset_holder(size_type(hdr_addr - mem_address)); + chain.incorporate_after + (chain.last(), void_pointer(mem_address + HdrOffsetSize), m_real_node_size, elements_per_subblock); + } + chain.incorporate_after(chain.last(), void_pointer(hdr_addr + HdrSize), m_real_node_size, hdr_subblock_elements); + } + else{ + for( size_type subblock = 0, maxsubblock = m_num_subblocks - 1 + ; subblock < maxsubblock + ; ++subblock, mem_address += m_real_block_alignment){ + //Initialize header offset mark + new(mem_address) hdr_offset_holder(size_type(hdr_addr - mem_address)); + this->priv_dispatch_block_chain_or_free + (chain, c_info, elements_per_subblock, mem_address + HdrOffsetSize, total_elements, false); + } + this->priv_dispatch_block_chain_or_free + (chain, c_info, hdr_subblock_elements, hdr_addr + HdrSize, total_elements, true); + } + } + } + + private: + typedef typename boost::intrusive::pointer_traits + ::template rebind_pointer::type segment_mngr_base_ptr_t; + const size_type m_max_free_blocks; + const size_type m_real_node_size; + //Round the size to a power of two value. + //This is the total memory size (including payload) that we want to + //allocate from the general-purpose allocator + const size_type m_real_block_alignment; + size_type m_num_subblocks; + //This is the real number of nodes per block + //const + size_type m_real_num_node; + segment_mngr_base_ptr_t mp_segment_mngr_base; //Segment manager + block_container_t m_block_container; //Intrusive block list + size_type m_totally_free_blocks; //Free blocks +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP diff --git a/boost/container/detail/advanced_insert_int.hpp b/boost/container/detail/advanced_insert_int.hpp new file mode 100644 index 0000000..a35279d --- /dev/null +++ b/boost/container/detail/advanced_insert_int.hpp @@ -0,0 +1,477 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP +#define BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include //std::iterator_traits +#include +#include + +namespace boost { namespace container { namespace container_detail { + +template +struct move_insert_range_proxy +{ + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::value_type value_type; + + explicit move_insert_range_proxy(FwdIt first) + : first_(first) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) + { + this->first_ = ::boost::container::uninitialized_move_alloc_n_source + (a, this->first_, n, p); + } + + void copy_n_and_update(A &, Iterator p, size_type n) + { + this->first_ = ::boost::container::move_n_source(this->first_, n, p); + } + + FwdIt first_; +}; + + +template +struct insert_range_proxy +{ + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::value_type value_type; + + explicit insert_range_proxy(FwdIt first) + : first_(first) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) + { + this->first_ = ::boost::container::uninitialized_copy_alloc_n_source(a, this->first_, n, p); + } + + void copy_n_and_update(A &, Iterator p, size_type n) + { + this->first_ = ::boost::container::copy_n_source(this->first_, n, p); + } + + FwdIt first_; +}; + + +template +struct insert_n_copies_proxy +{ + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::value_type value_type; + + explicit insert_n_copies_proxy(const value_type &v) + : v_(v) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) const + { boost::container::uninitialized_fill_alloc_n(a, v_, n, p); } + + void copy_n_and_update(A &, Iterator p, size_type n) const + { + for (; 0 < n; --n, ++p){ + *p = v_; + } + } + + const value_type &v_; +}; + +template +struct insert_value_initialized_n_proxy +{ + typedef ::boost::container::allocator_traits alloc_traits; + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::value_type value_type; + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) const + { boost::container::uninitialized_value_init_alloc_n(a, n, p); } + + void copy_n_and_update(A &, Iterator, size_type) const + { BOOST_ASSERT(false); } +}; + +template +struct insert_default_initialized_n_proxy +{ + typedef ::boost::container::allocator_traits alloc_traits; + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::value_type value_type; + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) const + { boost::container::uninitialized_default_init_alloc_n(a, n, p); } + + void copy_n_and_update(A &, Iterator, size_type) const + { BOOST_ASSERT(false); } +}; + +template +struct insert_copy_proxy +{ + typedef boost::container::allocator_traits alloc_traits; + typedef typename alloc_traits::size_type size_type; + typedef typename alloc_traits::value_type value_type; + + explicit insert_copy_proxy(const value_type &v) + : v_(v) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) const + { + BOOST_ASSERT(n == 1); (void)n; + alloc_traits::construct( a, iterator_to_raw_pointer(p), v_); + } + + void copy_n_and_update(A &, Iterator p, size_type n) const + { + BOOST_ASSERT(n == 1); (void)n; + *p =v_; + } + + const value_type &v_; +}; + + +template +struct insert_move_proxy +{ + typedef boost::container::allocator_traits alloc_traits; + typedef typename alloc_traits::size_type size_type; + typedef typename alloc_traits::value_type value_type; + + explicit insert_move_proxy(value_type &v) + : v_(v) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) const + { + BOOST_ASSERT(n == 1); (void)n; + alloc_traits::construct( a, iterator_to_raw_pointer(p), ::boost::move(v_) ); + } + + void copy_n_and_update(A &, Iterator p, size_type n) const + { + BOOST_ASSERT(n == 1); (void)n; + *p = ::boost::move(v_); + } + + value_type &v_; +}; + +template +insert_move_proxy get_insert_value_proxy(BOOST_RV_REF(typename std::iterator_traits::value_type) v) +{ + return insert_move_proxy(v); +} + +template +insert_copy_proxy get_insert_value_proxy(const typename std::iterator_traits::value_type &v) +{ + return insert_copy_proxy(v); +} + +}}} //namespace boost { namespace container { namespace container_detail { + +#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + +#include +#include +#include +//#include //For debugging purposes + +namespace boost { +namespace container { +namespace container_detail { + +template +struct insert_non_movable_emplace_proxy +{ + typedef boost::container::allocator_traits alloc_traits; + typedef typename alloc_traits::size_type size_type; + typedef typename alloc_traits::value_type value_type; + + typedef typename build_number_seq::type index_tuple_t; + + explicit insert_non_movable_emplace_proxy(Args&&... args) + : args_(args...) + {} + + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) + { this->priv_uninitialized_copy_some_and_update(a, index_tuple_t(), p, n); } + + private: + template + void priv_uninitialized_copy_some_and_update(A &a, const index_tuple&, Iterator p, size_type n) + { + BOOST_ASSERT(n == 1); (void)n; + alloc_traits::construct( a, iterator_to_raw_pointer(p), ::boost::forward(get(this->args_))... ); + } + + protected: + tuple args_; +}; + +template +struct insert_emplace_proxy + : public insert_non_movable_emplace_proxy +{ + typedef insert_non_movable_emplace_proxy base_t; + typedef boost::container::allocator_traits alloc_traits; + typedef typename base_t::value_type value_type; + typedef typename base_t::size_type size_type; + typedef typename base_t::index_tuple_t index_tuple_t; + + explicit insert_emplace_proxy(Args&&... args) + : base_t(::boost::forward(args)...) + {} + + void copy_n_and_update(A &a, Iterator p, size_type n) + { this->priv_copy_some_and_update(a, index_tuple_t(), p, n); } + + private: + + template + void priv_copy_some_and_update(A &a, const index_tuple&, Iterator p, size_type n) + { + BOOST_ASSERT(n ==1); (void)n; + aligned_storage::value> v; + value_type *vp = static_cast(static_cast(&v)); + alloc_traits::construct(a, vp, + ::boost::forward(get(this->args_))...); + BOOST_TRY{ + *p = ::boost::move(*vp); + } + BOOST_CATCH(...){ + alloc_traits::destroy(a, vp); + BOOST_RETHROW + } + BOOST_CATCH_END + alloc_traits::destroy(a, vp); + } +}; + +//Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type +template +struct insert_emplace_proxy::value_type> + : public insert_move_proxy +{ + explicit insert_emplace_proxy(typename boost::container::allocator_traits::value_type &&v) + : insert_move_proxy(v) + {} +}; + +//We use "add_const" here as adding "const" only confuses MSVC12(and maybe later) provoking +//compiler error C2752 (“more than one partial specialization matches”). +//Any problem is solvable with an extra layer of indirection? ;-) +template +struct insert_emplace_proxy::value_type>::type + > + : public insert_copy_proxy +{ + explicit insert_emplace_proxy(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +template +struct insert_emplace_proxy::value_type &> + : public insert_copy_proxy +{ + explicit insert_emplace_proxy(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +template +struct insert_emplace_proxy::value_type>::type & + > + : public insert_copy_proxy +{ + explicit insert_emplace_proxy(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +}}} //namespace boost { namespace container { namespace container_detail { + +#else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +#define BOOST_PP_LOCAL_MACRO(N) \ +template \ +struct BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \ +{ \ + typedef boost::container::allocator_traits alloc_traits; \ + typedef typename alloc_traits::size_type size_type; \ + typedef typename alloc_traits::value_type value_type; \ + \ + explicit BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \ + ( BOOST_PP_ENUM(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \ + BOOST_PP_EXPR_IF(N, :) BOOST_PP_ENUM(N, BOOST_CONTAINER_PP_PARAM_INIT, _) \ + {} \ + \ + void uninitialized_copy_n_and_update(A &a, Iterator p, size_type n) \ + { \ + BOOST_ASSERT(n == 1); (void)n; \ + alloc_traits::construct \ + ( a, iterator_to_raw_pointer(p) \ + BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) \ + ); \ + } \ + \ + void copy_n_and_update(A &, Iterator, size_type) \ + { BOOST_ASSERT(false); } \ + \ + protected: \ + BOOST_PP_REPEAT(N, BOOST_CONTAINER_PP_PARAM_DEFINE, _) \ +}; \ + \ +template \ +struct BOOST_PP_CAT(insert_emplace_proxy_arg, N) \ + : BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \ + < A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, P) > \ +{ \ + typedef BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \ + base_t; \ + typedef typename base_t::value_type value_type; \ + typedef typename base_t::size_type size_type; \ + typedef boost::container::allocator_traits alloc_traits; \ + \ + explicit BOOST_PP_CAT(insert_emplace_proxy_arg, N) \ + ( BOOST_PP_ENUM(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \ + : base_t(BOOST_PP_ENUM(N, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ) \ + {} \ + \ + void copy_n_and_update(A &a, Iterator p, size_type n) \ + { \ + BOOST_ASSERT(n == 1); (void)n; \ + aligned_storage::value> v; \ + value_type *vp = static_cast(static_cast(&v)); \ + alloc_traits::construct(a, vp \ + BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _)); \ + BOOST_TRY{ \ + *p = ::boost::move(*vp); \ + } \ + BOOST_CATCH(...){ \ + alloc_traits::destroy(a, vp); \ + BOOST_RETHROW \ + } \ + BOOST_CATCH_END \ + alloc_traits::destroy(a, vp); \ + } \ +}; \ +//! +#define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) +#include BOOST_PP_LOCAL_ITERATE() + +#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + +//Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type +template +struct insert_emplace_proxy_arg1::value_type> > + : public insert_move_proxy +{ + explicit insert_emplace_proxy_arg1(typename boost::container::allocator_traits::value_type &v) + : insert_move_proxy(v) + {} +}; + +template +struct insert_emplace_proxy_arg1::value_type> + : public insert_copy_proxy +{ + explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +#else //e.g. MSVC10 & MSVC11 + +//Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type +template +struct insert_emplace_proxy_arg1::value_type> + : public insert_move_proxy +{ + explicit insert_emplace_proxy_arg1(typename boost::container::allocator_traits::value_type &&v) + : insert_move_proxy(v) + {} +}; + +//We use "add_const" here as adding "const" only confuses MSVC10&11 provoking +//compiler error C2752 (“more than one partial specialization matches”). +//Any problem is solvable with an extra layer of indirection? ;-) +template +struct insert_emplace_proxy_arg1::value_type>::type + > + : public insert_copy_proxy +{ + explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +template +struct insert_emplace_proxy_arg1::value_type &> + : public insert_copy_proxy +{ + explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +template +struct insert_emplace_proxy_arg1::value_type>::type & + > + : public insert_copy_proxy +{ + explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits::value_type &v) + : insert_copy_proxy(v) + {} +}; + +#endif + +}}} //namespace boost { namespace container { namespace container_detail { + +#endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + +#include + +#endif //#ifndef BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP diff --git a/boost/container/detail/algorithms.hpp b/boost/container/detail/algorithms.hpp new file mode 100644 index 0000000..af15f65 --- /dev/null +++ b/boost/container/detail/algorithms.hpp @@ -0,0 +1,62 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_ALGORITHMS_HPP +#define BOOST_CONTAINER_DETAIL_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +namespace boost { +namespace container { + +template +inline void construct_in_place(A &a, T* dest, InpIt source) +{ boost::container::allocator_traits::construct(a, dest, *source); } + +template +inline void construct_in_place(A &a, T *dest, value_init_construct_iterator) +{ + boost::container::allocator_traits::construct(a, dest); +} + +template +class default_init_construct_iterator; + +template +inline void construct_in_place(A &a, T *dest, default_init_construct_iterator) +{ + boost::container::allocator_traits::construct(a, dest, default_init); +} + +template +class emplace_iterator; + +template +inline void construct_in_place(A &a, T *dest, emplace_iterator ei) +{ + ei.construct_in_place(a, dest); +} + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ALGORITHMS_HPP + diff --git a/boost/container/detail/alloc_lib.h b/boost/container/detail/alloc_lib.h new file mode 100644 index 0000000..4802d9d --- /dev/null +++ b/boost/container/detail/alloc_lib.h @@ -0,0 +1,326 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_CONTAINER_ALLOC_LIB_EXT_H +#define BOOST_CONTAINER_ALLOC_LIB_EXT_H + +#include + +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable : 4127) + +/* + we need to import/export our code only if the user has specifically + asked for it by defining either BOOST_ALL_DYN_LINK if they want all boost + libraries to be dynamically linked, or BOOST_CONTAINER_DYN_LINK + if they want just this one to be dynamically liked: +*/ +#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_CONTAINER_DYN_LINK) + +/* export if this is our own source, otherwise import: */ +#ifdef BOOST_CONTAINER_SOURCE +# define BOOST_CONTAINER_DECL __declspec(dllexport) +#else +# define BOOST_CONTAINER_DECL __declspec(dllimport) +#endif /* BOOST_CONTAINER_SOURCE */ +#endif /* DYN_LINK */ +#endif /* _MSC_VER */ + +/* if BOOST_CONTAINER_DECL isn't defined yet define it now: */ +#ifndef BOOST_CONTAINER_DECL +#define BOOST_CONTAINER_DECL +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/*!An forward iterator to traverse the elements of a memory chain container.*/ +typedef struct multialloc_node_impl +{ + struct multialloc_node_impl *next_node_ptr; +} boost_cont_memchain_node; + + +/*!An forward iterator to traverse the elements of a memory chain container.*/ +typedef struct multialloc_it_impl +{ + boost_cont_memchain_node *node_ptr; +} boost_cont_memchain_it; + +/*!Memory chain: A container holding memory portions allocated by boost_cont_multialloc_nodes + and boost_cont_multialloc_arrays functions.*/ +typedef struct boost_cont_memchain_impl +{ + size_t num_mem; + boost_cont_memchain_node root_node; + boost_cont_memchain_node *last_node_ptr; +} boost_cont_memchain; + +/*!Advances the iterator one position so that it points to the next element in the memory chain*/ +#define BOOST_CONTAINER_MEMIT_NEXT(IT) (IT.node_ptr = IT.node_ptr->next_node_ptr) + +/*!Returns the address of the memory chain currently pointed by the iterator*/ +#define BOOST_CONTAINER_MEMIT_ADDR(IT) ((void*)IT.node_ptr) + +/*!Initializer for an iterator pointing to the position before the first element*/ +#define BOOST_CONTAINER_MEMCHAIN_BEFORE_BEGIN_IT(PMEMCHAIN) { &((PMEMCHAIN)->root_node) } + +/*!Initializer for an iterator pointing to the first element*/ +#define BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(PMEMCHAIN) {(PMEMCHAIN)->root_node.next_node_ptr } + +/*!Initializer for an iterator pointing to the last element*/ +#define BOOST_CONTAINER_MEMCHAIN_LAST_IT(PMEMCHAIN) {(PMEMCHAIN)->last_node_ptr } + +/*!Initializer for an iterator pointing to one past the last element (end iterator)*/ +#define BOOST_CONTAINER_MEMCHAIN_END_IT(PMEMCHAIN) {(boost_cont_memchain_node *)0 } + +/*!True if IT is the end iterator, false otherwise*/ +#define BOOST_CONTAINER_MEMCHAIN_IS_END_IT(PMEMCHAIN, IT) (!(IT).node_ptr) + +/*!The address of the first memory portion hold by the memory chain*/ +#define BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(PMEMCHAIN)((void*)((PMEMCHAIN)->root_node.next_node_ptr)) + +/*!The address of the last memory portion hold by the memory chain*/ +#define BOOST_CONTAINER_MEMCHAIN_LASTMEM(PMEMCHAIN) ((void*)((PMEMCHAIN)->last_node_ptr)) + +/*!The number of memory portions hold by the memory chain*/ +#define BOOST_CONTAINER_MEMCHAIN_SIZE(PMEMCHAIN) ((PMEMCHAIN)->num_mem) + +/*!Initializes the memory chain from the first memory portion, the last memory + portion and number of portions obtained from another memory chain*/ +#define BOOST_CONTAINER_MEMCHAIN_INIT_FROM(PMEMCHAIN, FIRST, LAST, NUM)\ + (PMEMCHAIN)->last_node_ptr = (boost_cont_memchain_node *)(LAST), \ + (PMEMCHAIN)->root_node.next_node_ptr = (boost_cont_memchain_node *)(FIRST), \ + (PMEMCHAIN)->num_mem = (NUM);\ +/**/ + +/*!Default initializes a memory chain. Postconditions: begin iterator is end iterator, + the number of portions is zero.*/ +#define BOOST_CONTAINER_MEMCHAIN_INIT(PMEMCHAIN)\ + ((PMEMCHAIN)->root_node.next_node_ptr = 0, (PMEMCHAIN)->last_node_ptr = &((PMEMCHAIN)->root_node), (PMEMCHAIN)->num_mem = 0)\ +/**/ + +/*!True if the memory chain is empty (holds no memory portions*/ +#define BOOST_CONTAINER_MEMCHAIN_EMPTY(PMEMCHAIN)\ + ((PMEMCHAIN)->num_mem == 0)\ +/**/ + +/*!Inserts a new memory portions in the front of the chain*/ +#define BOOST_CONTAINER_MEMCHAIN_PUSH_BACK(PMEMCHAIN, MEM)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\ + ____chain____->last_node_ptr->next_node_ptr = ____tmp_mem____;\ + ____tmp_mem____->next_node_ptr = 0;\ + ____chain____->last_node_ptr = ____tmp_mem____;\ + ++____chain____->num_mem;\ + }while(0)\ +/**/ + +/*!Inserts a new memory portions in the back of the chain*/ +#define BOOST_CONTAINER_MEMCHAIN_PUSH_FRONT(PMEMCHAIN, MEM)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain_node *____tmp_mem____ = (boost_cont_memchain_node *)(MEM);\ + boost_cont_memchain *____root____ = &((PMEMCHAIN)->root_node);\ + if(!____chain____->root_node.next_node_ptr){\ + ____chain____->last_node_ptr = ____tmp_mem____;\ + }\ + boost_cont_memchain_node *____old_first____ = ____root____->next_node_ptr;\ + ____tmp_mem____->next_node_ptr = ____old_first____;\ + ____root____->next_node_ptr = ____tmp_mem____;\ + ++____chain____->num_mem;\ + }while(0)\ +/**/ + +/*!Erases the memory portion after the portion pointed by BEFORE_IT from the memory chain*/ +/*!Precondition: BEFORE_IT must be a valid iterator of the memory chain and it can't be the end iterator*/ +#define BOOST_CONTAINER_MEMCHAIN_ERASE_AFTER(PMEMCHAIN, BEFORE_IT)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain_node *____prev_node____ = (BEFORE_IT).node_ptr;\ + boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\ + if(____chain____->last_node_ptr == ____erase_node____){\ + ____chain____->last_node_ptr = &____chain____->root_node;\ + }\ + ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\ + --____chain____->num_mem;\ + }while(0)\ +/**/ + +/*!Erases the first portion from the memory chain. + Precondition: the memory chain must not be empty*/ +#define BOOST_CONTAINER_MEMCHAIN_POP_FRONT(PMEMCHAIN)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain_node *____prev_node____ = &____chain____->root_node;\ + boost_cont_memchain_node *____erase_node____ = ____prev_node____->next_node_ptr;\ + if(____chain____->last_node_ptr == ____erase_node____){\ + ____chain____->last_node_ptr = &____chain____->root_node;\ + }\ + ____prev_node____->next_node_ptr = ____erase_node____->next_node_ptr;\ + --____chain____->num_mem;\ + }while(0)\ +/**/ + +/*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/ +/* +#define BOOST_CONTAINER_MEMCHAIN_SPLICE_BACK(PMEMCHAIN, PMEMCHAIN2)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain *____chain2____ = (PMEMCHAIN2);\ + if(!____chain2____->root_node.next_node_ptr){\ + break;\ + }\ + else if(!____chain____->first_mem){\ + ____chain____->first_mem = ____chain2____->first_mem;\ + ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\ + ____chain____->num_mem = ____chain2____->num_mem;\ + BOOST_CONTAINER_MEMCHAIN_INIT(*____chain2____);\ + }\ + else{\ + ____chain____->last_node_ptr->next_node_ptr = ____chain2____->first_mem;\ + ____chain____->last_node_ptr = ____chain2____->last_node_ptr;\ + ____chain____->num_mem += ____chain2____->num_mem;\ + }\ + }while(0)\*/ +/**/ + +/*!Joins two memory chains inserting the portions of the second chain at the back of the first chain*/ +#define BOOST_CONTAINER_MEMCHAIN_INCORPORATE_AFTER(PMEMCHAIN, BEFORE_IT, FIRST, BEFORELAST, NUM)\ + do{\ + boost_cont_memchain *____chain____ = (PMEMCHAIN);\ + boost_cont_memchain_node *____pnode____ = (BEFORE_IT).node_ptr;\ + boost_cont_memchain_node *____next____ = ____pnode____->next_node_ptr;\ + boost_cont_memchain_node *____first____ = (boost_cont_memchain_node *)(FIRST);\ + boost_cont_memchain_node *____blast____ = (boost_cont_memchain_node *)(BEFORELAST);\ + size_t ____num____ = (NUM);\ + if(!____num____){\ + break;\ + }\ + if(____pnode____ == ____chain____->last_node_ptr){\ + ____chain____->last_node_ptr = ____blast____;\ + }\ + ____pnode____->next_node_ptr = ____first____;\ + ____blast____->next_node_ptr = ____next____;\ + ____chain____->num_mem += ____num____;\ + }while(0)\ +/**/ + +BOOST_CONTAINER_DECL size_t boost_cont_size(const void *p); + +BOOST_CONTAINER_DECL void* boost_cont_malloc(size_t bytes); + +BOOST_CONTAINER_DECL void boost_cont_free(void* mem); + +BOOST_CONTAINER_DECL void* boost_cont_memalign(size_t bytes, size_t alignment); + +/*!Indicates the all elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays + must be contiguous.*/ +#define DL_MULTIALLOC_ALL_CONTIGUOUS ((size_t)(-1)) + +/*!Indicates the number of contiguous elements allocated by boost_cont_multialloc_nodes or boost_cont_multialloc_arrays + should be selected by those functions.*/ +#define DL_MULTIALLOC_DEFAULT_CONTIGUOUS ((size_t)(0)) + +BOOST_CONTAINER_DECL int boost_cont_multialloc_nodes + (size_t n_elements, size_t elem_size, size_t contiguous_elements, boost_cont_memchain *pchain); + +BOOST_CONTAINER_DECL int boost_cont_multialloc_arrays + (size_t n_elements, const size_t *sizes, size_t sizeof_element, size_t contiguous_elements, boost_cont_memchain *pchain); + +BOOST_CONTAINER_DECL void boost_cont_multidealloc(boost_cont_memchain *pchain); + +BOOST_CONTAINER_DECL size_t boost_cont_footprint(); + +BOOST_CONTAINER_DECL size_t boost_cont_allocated_memory(); + +BOOST_CONTAINER_DECL size_t boost_cont_chunksize(const void *p); + +BOOST_CONTAINER_DECL int boost_cont_all_deallocated(); + +typedef struct boost_cont_malloc_stats_impl +{ + size_t max_system_bytes; + size_t system_bytes; + size_t in_use_bytes; +} boost_cont_malloc_stats_t; + +BOOST_CONTAINER_DECL boost_cont_malloc_stats_t boost_cont_malloc_stats(); + +BOOST_CONTAINER_DECL size_t boost_cont_in_use_memory(); + +BOOST_CONTAINER_DECL int boost_cont_trim(size_t pad); + +BOOST_CONTAINER_DECL int boost_cont_mallopt + (int parameter_number, int parameter_value); + +BOOST_CONTAINER_DECL int boost_cont_grow + (void* oldmem, size_t minbytes, size_t maxbytes, size_t *received); + +BOOST_CONTAINER_DECL int boost_cont_shrink + (void* oldmem, size_t minbytes, size_t maxbytes, size_t *received, int do_commit); + +BOOST_CONTAINER_DECL void* boost_cont_alloc + (size_t minbytes, size_t preferred_bytes, size_t *received_bytes); + +BOOST_CONTAINER_DECL int boost_cont_malloc_check(); + +typedef unsigned int allocation_type; + +enum +{ + // constants for allocation commands + BOOST_CONTAINER_ALLOCATE_NEW = 0X01, + BOOST_CONTAINER_EXPAND_FWD = 0X02, + BOOST_CONTAINER_EXPAND_BWD = 0X04, + BOOST_CONTAINER_SHRINK_IN_PLACE = 0X08, + BOOST_CONTAINER_NOTHROW_ALLOCATION = 0X10, +// BOOST_CONTAINER_ZERO_MEMORY = 0X20, + BOOST_CONTAINER_TRY_SHRINK_IN_PLACE = 0X40, + BOOST_CONTAINER_EXPAND_BOTH = BOOST_CONTAINER_EXPAND_FWD | BOOST_CONTAINER_EXPAND_BWD, + BOOST_CONTAINER_EXPAND_OR_NEW = BOOST_CONTAINER_ALLOCATE_NEW | BOOST_CONTAINER_EXPAND_BOTH +}; + +//#define BOOST_CONTAINERDLMALLOC__FOOTERS +#ifndef BOOST_CONTAINERDLMALLOC__FOOTERS +enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD = sizeof(size_t) }; +#else +enum { BOOST_CONTAINER_ALLOCATION_PAYLOAD = sizeof(size_t)*2 }; +#endif + +typedef struct boost_cont_command_ret_impl +{ + void *first; + int second; +}boost_cont_command_ret_t; + +BOOST_CONTAINER_DECL boost_cont_command_ret_t boost_cont_allocation_command + ( allocation_type command + , size_t sizeof_object + , size_t limit_objects + , size_t preferred_objects + , size_t *received_objects + , void *reuse_ptr + ); + +BOOST_CONTAINER_DECL int boost_cont_mallopt(int param_number, int value); + +#ifdef __cplusplus +} //extern "C" { +#endif + +#ifdef _MSC_VER +#pragma warning (pop) +#endif + + +#endif //#define BOOST_CONTAINERDLMALLOC__EXT_H diff --git a/boost/container/detail/alloc_lib_auto_link.hpp b/boost/container/detail/alloc_lib_auto_link.hpp new file mode 100644 index 0000000..e0a01b6 --- /dev/null +++ b/boost/container/detail/alloc_lib_auto_link.hpp @@ -0,0 +1,20 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_CONTAINER_DETAIL_BOOST_CONT_EXT_AUTO_LINK_HPP +#define BOOST_CONTAINER_DETAIL_BOOST_CONT_EXT_AUTO_LINK_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_BOOST_CONT_EXT_AUTO_LINK_HPP diff --git a/boost/container/detail/allocation_type.hpp b/boost/container/detail/allocation_type.hpp new file mode 100644 index 0000000..65d543a --- /dev/null +++ b/boost/container/detail/allocation_type.hpp @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATION_TYPE_HPP +#define BOOST_CONTAINER_ALLOCATION_TYPE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +enum allocation_type_v +{ + // constants for allocation commands + allocate_new_v = 0x01, + expand_fwd_v = 0x02, + expand_bwd_v = 0x04, +// expand_both = expand_fwd | expand_bwd, +// expand_or_new = allocate_new | expand_both, + shrink_in_place_v = 0x08, + nothrow_allocation_v = 0x10, + zero_memory_v = 0x20, + try_shrink_in_place_v = 0x40 +}; + +typedef int allocation_type; +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +static const allocation_type allocate_new = (allocation_type)allocate_new_v; +static const allocation_type expand_fwd = (allocation_type)expand_fwd_v; +static const allocation_type expand_bwd = (allocation_type)expand_bwd_v; +static const allocation_type shrink_in_place = (allocation_type)shrink_in_place_v; +static const allocation_type try_shrink_in_place= (allocation_type)try_shrink_in_place_v; +static const allocation_type nothrow_allocation = (allocation_type)nothrow_allocation_v; +static const allocation_type zero_memory = (allocation_type)zero_memory_v; + +} //namespace container { +} //namespace boost { + +#include + +#endif //BOOST_CONTAINER_ALLOCATION_TYPE_HPP diff --git a/boost/container/detail/allocator_version_traits.hpp b/boost/container/detail/allocator_version_traits.hpp new file mode 100644 index 0000000..18bb2ac --- /dev/null +++ b/boost/container/detail/allocator_version_traits.hpp @@ -0,0 +1,168 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP +#define BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include //allocator_traits +#include +#include //multiallocation_chain +#include //version_type +#include //allocation_type +#include //integral_constant +#include //pointer_traits +#include //pair +#include //BOOST_TRY + +namespace boost { +namespace container { +namespace container_detail { + +template::value> +struct allocator_version_traits +{ + typedef ::boost::container::container_detail::integral_constant + alloc_version; + + typedef typename Allocator::multiallocation_chain multiallocation_chain; + + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::size_type size_type; + + //Node allocation interface + static pointer allocate_one(Allocator &a) + { return a.allocate_one(); } + + static void deallocate_one(Allocator &a, const pointer &p) + { a.deallocate_one(p); } + + static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m) + { return a.allocate_individual(n, m); } + + static void deallocate_individual(Allocator &a, multiallocation_chain &holder) + { a.deallocate_individual(holder); } + + static std::pair + allocation_command(Allocator &a, allocation_type command, + size_type limit_size, size_type preferred_size, + size_type &received_size, const pointer &reuse) + { + return a.allocation_command + (command, limit_size, preferred_size, received_size, reuse); + } +}; + +template +struct allocator_version_traits +{ + typedef ::boost::container::container_detail::integral_constant + alloc_version; + + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::size_type size_type; + typedef typename boost::container::allocator_traits::value_type value_type; + + typedef typename boost::intrusive::pointer_traits:: + template rebind_pointer::type void_ptr; + typedef container_detail::basic_multiallocation_chain + multialloc_cached_counted; + typedef boost::container::container_detail:: + transform_multiallocation_chain + < multialloc_cached_counted, value_type> multiallocation_chain; + + //Node allocation interface + static pointer allocate_one(Allocator &a) + { return a.allocate(1); } + + static void deallocate_one(Allocator &a, const pointer &p) + { a.deallocate(p, 1); } + + static void deallocate_individual(Allocator &a, multiallocation_chain &holder) + { + size_type n = holder.size(); + typename multiallocation_chain::iterator it = holder.begin(); + while(n--){ + pointer p = boost::intrusive::pointer_traits::pointer_to(*it); + ++it; + a.deallocate(p, 1); + } + } + + struct allocate_individual_rollback + { + allocate_individual_rollback(Allocator &a, multiallocation_chain &chain) + : mr_a(a), mp_chain(&chain) + {} + + ~allocate_individual_rollback() + { + if(mp_chain) + allocator_version_traits::deallocate_individual(mr_a, *mp_chain); + } + + void release() + { + mp_chain = 0; + } + + Allocator &mr_a; + multiallocation_chain * mp_chain; + }; + + static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m) + { + allocate_individual_rollback rollback(a, m); + while(n--){ + m.push_front(a.allocate(1)); + } + rollback.release(); + } + + static std::pair + allocation_command(Allocator &a, allocation_type command, + size_type, size_type preferred_size, + size_type &received_size, const pointer &) + { + std::pair ret(pointer(), false); + if(!(command & allocate_new)){ + if(!(command & nothrow_allocation)){ + throw_logic_error("version 1 allocator without allocate_new flag"); + } + } + else{ + received_size = preferred_size; + BOOST_TRY{ + ret.first = a.allocate(received_size); + } + BOOST_CATCH(...){ + if(!(command & nothrow_allocation)){ + BOOST_RETHROW + } + } + BOOST_CATCH_END + } + return ret; + } +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif // ! defined(BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP) diff --git a/boost/container/detail/auto_link.hpp b/boost/container/detail/auto_link.hpp new file mode 100644 index 0000000..2e47333 --- /dev/null +++ b/boost/container/detail/auto_link.hpp @@ -0,0 +1,38 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_CONTAINER_DETAIL_AUTO_LINK_HPP_INCLUDED +#define BOOST_CONTAINER_DETAIL_AUTO_LINK_HPP_INCLUDED + +#if defined(_MSC_VER) +# pragma once +#endif + +// +// Automatically link to the correct build variant where possible. +// +#if !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_CONTAINER_NO_LIB) && !defined(BOOST_CONTAINER_SOURCE) +// +// Set the name of our library, this will get undef'ed by auto_link.hpp +// once it's done with it: +// +#define BOOST_LIB_NAME boost_container +// +// If we're importing code from a dll, then tell auto_link.hpp about it: +// +#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_CONTAINER_DYN_LINK) +# define BOOST_DYN_LINK +#endif +// +// And include the header that does the work: +// +#include +#endif // auto-linking disabled + +#endif //#ifndef BOOST_CONTAINER_DETAIL_AUTO_LINK_HPP_INCLUDED diff --git a/boost/container/detail/config_begin.hpp b/boost/container/detail/config_begin.hpp new file mode 100644 index 0000000..ca9dd59 --- /dev/null +++ b/boost/container/detail/config_begin.hpp @@ -0,0 +1,48 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_CONFIG_INCLUDED +#define BOOST_CONTAINER_CONTAINER_DETAIL_CONFIG_INCLUDED +#ifndef BOOST_CONFIG_HPP +#include +#endif + +#endif //BOOST_CONTAINER_CONTAINER_DETAIL_CONFIG_INCLUDED + +#ifdef BOOST_MSVC + #pragma warning (push) + #pragma warning (disable : 4702) // unreachable code + #pragma warning (disable : 4706) // assignment within conditional expression + #pragma warning (disable : 4127) // conditional expression is constant + #pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned + #pragma warning (disable : 4284) // odd return type for operator-> + #pragma warning (disable : 4244) // possible loss of data + #pragma warning (disable : 4251) // "identifier" : class "type" needs to have dll-interface to be used by clients of class "type2" + #pragma warning (disable : 4267) // conversion from "X" to "Y", possible loss of data + #pragma warning (disable : 4275) // non DLL-interface classkey "identifier" used as base for DLL-interface classkey "identifier" + #pragma warning (disable : 4355) // "this" : used in base member initializer list + #pragma warning (disable : 4503) // "identifier" : decorated name length exceeded, name was truncated + #pragma warning (disable : 4511) // copy constructor could not be generated + #pragma warning (disable : 4512) // assignment operator could not be generated + #pragma warning (disable : 4514) // unreferenced inline removed + #pragma warning (disable : 4521) // Disable "multiple copy constructors specified" + #pragma warning (disable : 4522) // "class" : multiple assignment operators specified + #pragma warning (disable : 4675) // "method" should be declared "static" and have exactly one parameter + #pragma warning (disable : 4710) // function not inlined + #pragma warning (disable : 4711) // function selected for automatic inline expansion + #pragma warning (disable : 4786) // identifier truncated in debug info + #pragma warning (disable : 4996) // "function": was declared deprecated + #pragma warning (disable : 4197) // top-level volatile in cast is ignored + #pragma warning (disable : 4541) // 'typeid' used on polymorphic type 'boost::exception' + // with /GR-; unpredictable behavior may result + #pragma warning (disable : 4673) // throwing '' the following types will not be considered at the catch site + #pragma warning (disable : 4671) // the copy constructor is inaccessible + #pragma warning (disable : 4584) // X is already a base-class of Y + #pragma warning (disable : 4510) // default constructor could not be generated +#endif //BOOST_MSVC diff --git a/boost/container/detail/config_end.hpp b/boost/container/detail/config_end.hpp new file mode 100644 index 0000000..f93c8f6 --- /dev/null +++ b/boost/container/detail/config_end.hpp @@ -0,0 +1,13 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#if defined BOOST_MSVC + #pragma warning (pop) +#endif + diff --git a/boost/container/detail/destroyers.hpp b/boost/container/detail/destroyers.hpp new file mode 100644 index 0000000..ea9b617 --- /dev/null +++ b/boost/container/detail/destroyers.hpp @@ -0,0 +1,380 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DESTROYERS_HPP +#define BOOST_CONTAINER_DESTROYERS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +//!A deleter for scoped_ptr that deallocates the memory +//!allocated for an object using a STL allocator. +template +struct scoped_deallocator +{ + typedef allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::pointer pointer; + typedef container_detail::integral_constant::value> alloc_version; + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + + private: + void priv_deallocate(allocator_v1) + { m_alloc.deallocate(m_ptr, 1); } + + void priv_deallocate(allocator_v2) + { m_alloc.deallocate_one(m_ptr); } + + BOOST_MOVABLE_BUT_NOT_COPYABLE(scoped_deallocator) + + public: + + pointer m_ptr; + A& m_alloc; + + scoped_deallocator(pointer p, A& a) + : m_ptr(p), m_alloc(a) + {} + + ~scoped_deallocator() + { if (m_ptr)priv_deallocate(alloc_version()); } + + scoped_deallocator(BOOST_RV_REF(scoped_deallocator) o) + : m_ptr(o.m_ptr), m_alloc(o.m_alloc) + { o.release(); } + + pointer get() const + { return m_ptr; } + + void set(const pointer &p) + { m_ptr = p; } + + void release() + { m_ptr = 0; } +}; + +template +struct null_scoped_deallocator +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::size_type size_type; + + null_scoped_deallocator(pointer, Allocator&, size_type) + {} + + void release() + {} + + pointer get() const + { return pointer(); } + + void set(const pointer &) + {} +}; + +//!A deleter for scoped_ptr that deallocates the memory +//!allocated for an array of objects using a STL allocator. +template +struct scoped_array_deallocator +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::size_type size_type; + + scoped_array_deallocator(pointer p, Allocator& a, size_type length) + : m_ptr(p), m_alloc(a), m_length(length) {} + + ~scoped_array_deallocator() + { if (m_ptr) m_alloc.deallocate(m_ptr, m_length); } + + void release() + { m_ptr = 0; } + + private: + pointer m_ptr; + Allocator& m_alloc; + size_type m_length; +}; + +template +struct null_scoped_array_deallocator +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::size_type size_type; + + null_scoped_array_deallocator(pointer, Allocator&, size_type) + {} + + void release() + {} +}; + +template +struct scoped_destroy_deallocator +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::size_type size_type; + typedef container_detail::integral_constant::value> alloc_version; + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + + scoped_destroy_deallocator(pointer p, Allocator& a) + : m_ptr(p), m_alloc(a) {} + + ~scoped_destroy_deallocator() + { + if(m_ptr){ + AllocTraits::destroy(m_alloc, container_detail::to_raw_pointer(m_ptr)); + priv_deallocate(m_ptr, alloc_version()); + } + } + + void release() + { m_ptr = 0; } + + private: + + void priv_deallocate(const pointer &p, allocator_v1) + { AllocTraits::deallocate(m_alloc, p, 1); } + + void priv_deallocate(const pointer &p, allocator_v2) + { m_alloc.deallocate_one(p); } + + pointer m_ptr; + Allocator& m_alloc; +}; + + +//!A deleter for scoped_ptr that destroys +//!an object using a STL allocator. +template +struct scoped_destructor_n +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::value_type value_type; + typedef typename AllocTraits::size_type size_type; + + scoped_destructor_n(pointer p, Allocator& a, size_type n) + : m_p(p), m_a(a), m_n(n) + {} + + void release() + { m_p = 0; } + + void increment_size(size_type inc) + { m_n += inc; } + + void increment_size_backwards(size_type inc) + { m_n += inc; m_p -= inc; } + + void shrink_forward(size_type inc) + { m_n -= inc; m_p += inc; } + + ~scoped_destructor_n() + { + if(!m_p) return; + value_type *raw_ptr = container_detail::to_raw_pointer(m_p); + while(m_n--){ + AllocTraits::destroy(m_a, raw_ptr++); + } + } + + private: + pointer m_p; + Allocator & m_a; + size_type m_n; +}; + +//!A deleter for scoped_ptr that destroys +//!an object using a STL allocator. +template +struct null_scoped_destructor_n +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::pointer pointer; + typedef typename AllocTraits::size_type size_type; + + null_scoped_destructor_n(pointer, Allocator&, size_type) + {} + + void increment_size(size_type) + {} + + void increment_size_backwards(size_type) + {} + + void shrink_forward(size_type) + {} + + void release() + {} +}; + +template +class scoped_destructor +{ + typedef boost::container::allocator_traits AllocTraits; + public: + typedef typename A::value_type value_type; + scoped_destructor(A &a, value_type *pv) + : pv_(pv), a_(a) + {} + + ~scoped_destructor() + { + if(pv_){ + AllocTraits::destroy(a_, pv_); + } + } + + void release() + { pv_ = 0; } + + + void set(value_type *ptr) { pv_ = ptr; } + + value_type *get() const { return pv_; } + + private: + value_type *pv_; + A &a_; +}; + + +template +class value_destructor +{ + typedef boost::container::allocator_traits AllocTraits; + public: + typedef typename A::value_type value_type; + value_destructor(A &a, value_type &rv) + : rv_(rv), a_(a) + {} + + ~value_destructor() + { + AllocTraits::destroy(a_, &rv_); + } + + private: + value_type &rv_; + A &a_; +}; + +template +class allocator_destroyer +{ + typedef boost::container::allocator_traits AllocTraits; + typedef typename AllocTraits::value_type value_type; + typedef typename AllocTraits::pointer pointer; + typedef container_detail::integral_constant::value> alloc_version; + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + + private: + Allocator & a_; + + private: + void priv_deallocate(const pointer &p, allocator_v1) + { AllocTraits::deallocate(a_,p, 1); } + + void priv_deallocate(const pointer &p, allocator_v2) + { a_.deallocate_one(p); } + + public: + allocator_destroyer(Allocator &a) + : a_(a) + {} + + void operator()(const pointer &p) + { + AllocTraits::destroy(a_, container_detail::to_raw_pointer(p)); + this->priv_deallocate(p, alloc_version()); + } +}; + +template +class allocator_destroyer_and_chain_builder +{ + typedef allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::value_type value_type; + typedef typename A::multiallocation_chain multiallocation_chain; + + A & a_; + multiallocation_chain &c_; + + public: + allocator_destroyer_and_chain_builder(A &a, multiallocation_chain &c) + : a_(a), c_(c) + {} + + void operator()(const typename A::pointer &p) + { + allocator_traits::destroy(a_, container_detail::to_raw_pointer(p)); + c_.push_back(p); + } +}; + +template +class allocator_multialloc_chain_node_deallocator +{ + typedef allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::value_type value_type; + typedef typename A::multiallocation_chain multiallocation_chain; + typedef allocator_destroyer_and_chain_builder chain_builder; + + A & a_; + multiallocation_chain c_; + + public: + allocator_multialloc_chain_node_deallocator(A &a) + : a_(a), c_() + {} + + chain_builder get_chain_builder() + { return chain_builder(a_, c_); } + + ~allocator_multialloc_chain_node_deallocator() + { + a_.deallocate_individual(c_); + } +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DESTROYERS_HPP diff --git a/boost/container/detail/flat_tree.hpp b/boost/container/detail/flat_tree.hpp new file mode 100644 index 0000000..a6f7568 --- /dev/null +++ b/boost/container/detail/flat_tree.hpp @@ -0,0 +1,1021 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +//////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_FLAT_TREE_HPP +#define BOOST_CONTAINER_FLAT_TREE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER +#include +#endif +#include +#include + +namespace boost { + +namespace container { + +namespace container_detail { + +template +class flat_tree_value_compare + : private Compare +{ + typedef Value first_argument_type; + typedef Value second_argument_type; + typedef bool return_type; + public: + flat_tree_value_compare() + : Compare() + {} + + flat_tree_value_compare(const Compare &pred) + : Compare(pred) + {} + + bool operator()(const Value& lhs, const Value& rhs) const + { + KeyOfValue key_extract; + return Compare::operator()(key_extract(lhs), key_extract(rhs)); + } + + const Compare &get_comp() const + { return *this; } + + Compare &get_comp() + { return *this; } +}; + +template +struct get_flat_tree_iterators +{ + #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + typedef Pointer iterator; + typedef typename boost::intrusive:: + pointer_traits::element_type iterator_element_type; + typedef typename boost::intrusive:: + pointer_traits:: template + rebind_pointer::type const_iterator; + #else //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + typedef typename boost::container::container_detail:: + vec_iterator iterator; + typedef typename boost::container::container_detail:: + vec_iterator const_iterator; + #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + typedef boost::container::container_detail:: + reverse_iterator reverse_iterator; + typedef boost::container::container_detail:: + reverse_iterator const_reverse_iterator; +}; + +template +class flat_tree +{ + typedef boost::container::vector vector_t; + typedef A allocator_t; + + public: + typedef flat_tree_value_compare value_compare; + + private: + struct Data + //Inherit from value_compare to do EBO + : public value_compare + { + BOOST_COPYABLE_AND_MOVABLE(Data) + + public: + Data() + : value_compare(), m_vect() + {} + + explicit Data(const Data &d) + : value_compare(static_cast(d)), m_vect(d.m_vect) + {} + + Data(BOOST_RV_REF(Data) d) + : value_compare(boost::move(static_cast(d))), m_vect(boost::move(d.m_vect)) + {} + + Data(const Data &d, const A &a) + : value_compare(static_cast(d)), m_vect(d.m_vect, a) + {} + + Data(BOOST_RV_REF(Data) d, const A &a) + : value_compare(boost::move(static_cast(d))), m_vect(boost::move(d.m_vect), a) + {} + + explicit Data(const Compare &comp) + : value_compare(comp), m_vect() + {} + + Data(const Compare &comp, const allocator_t &alloc) + : value_compare(comp), m_vect(alloc) + {} + + explicit Data(const allocator_t &alloc) + : value_compare(), m_vect(alloc) + {} + + Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d) + { + this->value_compare::operator=(d); + m_vect = d.m_vect; + return *this; + } + + Data& operator=(BOOST_RV_REF(Data) d) + { + this->value_compare::operator=(boost::move(static_cast(d))); + m_vect = boost::move(d.m_vect); + return *this; + } + + void swap(Data &d) + { + value_compare& mycomp = *this, & othercomp = d; + boost::container::swap_dispatch(mycomp, othercomp); + this->m_vect.swap(d.m_vect); + } + + vector_t m_vect; + }; + + Data m_data; + BOOST_COPYABLE_AND_MOVABLE(flat_tree) + + public: + + typedef typename vector_t::value_type value_type; + typedef typename vector_t::pointer pointer; + typedef typename vector_t::const_pointer const_pointer; + typedef typename vector_t::reference reference; + typedef typename vector_t::const_reference const_reference; + typedef Key key_type; + typedef Compare key_compare; + typedef typename vector_t::allocator_type allocator_type; + typedef typename vector_t::size_type size_type; + typedef typename vector_t::difference_type difference_type; + typedef typename vector_t::iterator iterator; + typedef typename vector_t::const_iterator const_iterator; + typedef typename vector_t::reverse_iterator reverse_iterator; + typedef typename vector_t::const_reverse_iterator const_reverse_iterator; + + //!Standard extension + typedef allocator_type stored_allocator_type; + + private: + typedef allocator_traits stored_allocator_traits; + + public: + flat_tree() + : m_data() + { } + + explicit flat_tree(const Compare& comp) + : m_data(comp) + { } + + flat_tree(const Compare& comp, const allocator_type& a) + : m_data(comp, a) + { } + + explicit flat_tree(const allocator_type& a) + : m_data(a) + { } + + flat_tree(const flat_tree& x) + : m_data(x.m_data) + { } + + flat_tree(BOOST_RV_REF(flat_tree) x) + : m_data(boost::move(x.m_data)) + { } + + flat_tree(const flat_tree& x, const allocator_type &a) + : m_data(x.m_data, a) + { } + + flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a) + : m_data(boost::move(x.m_data), a) + { } + + template + flat_tree( ordered_range_t, InputIterator first, InputIterator last + , const Compare& comp = Compare() + , const allocator_type& a = allocator_type()) + : m_data(comp, a) + { this->m_data.m_vect.insert(this->m_data.m_vect.end(), first, last); } + + template + flat_tree( bool unique_insertion + , InputIterator first, InputIterator last + , const Compare& comp = Compare() + , const allocator_type& a = allocator_type()) + : m_data(comp, a) + { + //Use cend() as hint to achieve linear time for + //ordered ranges as required by the standard + //for the constructor + //Call end() every iteration as reallocation might have invalidated iterators + if(unique_insertion){ + for ( ; first != last; ++first){ + this->insert_unique(this->cend(), *first); + } + } + else{ + for ( ; first != last; ++first){ + this->insert_equal(this->cend(), *first); + } + } + } + + ~flat_tree() + {} + + flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x) + { m_data = x.m_data; return *this; } + + flat_tree& operator=(BOOST_RV_REF(flat_tree) mx) + { m_data = boost::move(mx.m_data); return *this; } + + public: + // accessors: + Compare key_comp() const + { return this->m_data.get_comp(); } + + value_compare value_comp() const + { return this->m_data; } + + allocator_type get_allocator() const + { return this->m_data.m_vect.get_allocator(); } + + const stored_allocator_type &get_stored_allocator() const + { return this->m_data.m_vect.get_stored_allocator(); } + + stored_allocator_type &get_stored_allocator() + { return this->m_data.m_vect.get_stored_allocator(); } + + iterator begin() + { return this->m_data.m_vect.begin(); } + + const_iterator begin() const + { return this->cbegin(); } + + const_iterator cbegin() const + { return this->m_data.m_vect.begin(); } + + iterator end() + { return this->m_data.m_vect.end(); } + + const_iterator end() const + { return this->cend(); } + + const_iterator cend() const + { return this->m_data.m_vect.end(); } + + reverse_iterator rbegin() + { return reverse_iterator(this->end()); } + + const_reverse_iterator rbegin() const + { return this->crbegin(); } + + const_reverse_iterator crbegin() const + { return const_reverse_iterator(this->cend()); } + + reverse_iterator rend() + { return reverse_iterator(this->begin()); } + + const_reverse_iterator rend() const + { return this->crend(); } + + const_reverse_iterator crend() const + { return const_reverse_iterator(this->cbegin()); } + + bool empty() const + { return this->m_data.m_vect.empty(); } + + size_type size() const + { return this->m_data.m_vect.size(); } + + size_type max_size() const + { return this->m_data.m_vect.max_size(); } + + void swap(flat_tree& other) + { this->m_data.swap(other.m_data); } + + public: + // insert/erase + std::pair insert_unique(const value_type& val) + { + std::pair ret; + insert_commit_data data; + ret.second = this->priv_insert_unique_prepare(val, data); + ret.first = ret.second ? this->priv_insert_commit(data, val) + : iterator(vector_iterator_get_ptr(data.position)); + return ret; + } + + std::pair insert_unique(BOOST_RV_REF(value_type) val) + { + std::pair ret; + insert_commit_data data; + ret.second = this->priv_insert_unique_prepare(val, data); + ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val)) + : iterator(vector_iterator_get_ptr(data.position)); + return ret; + } + + iterator insert_equal(const value_type& val) + { + iterator i = this->upper_bound(KeyOfValue()(val)); + i = this->m_data.m_vect.insert(i, val); + return i; + } + + iterator insert_equal(BOOST_RV_REF(value_type) mval) + { + iterator i = this->upper_bound(KeyOfValue()(mval)); + i = this->m_data.m_vect.insert(i, boost::move(mval)); + return i; + } + + iterator insert_unique(const_iterator pos, const value_type& val) + { + std::pair ret; + insert_commit_data data; + return this->priv_insert_unique_prepare(pos, val, data) + ? this->priv_insert_commit(data, val) + : iterator(vector_iterator_get_ptr(data.position)); + } + + iterator insert_unique(const_iterator pos, BOOST_RV_REF(value_type) val) + { + std::pair ret; + insert_commit_data data; + return this->priv_insert_unique_prepare(pos, val, data) + ? this->priv_insert_commit(data, boost::move(val)) + : iterator(vector_iterator_get_ptr(data.position)); + } + + iterator insert_equal(const_iterator pos, const value_type& val) + { + insert_commit_data data; + this->priv_insert_equal_prepare(pos, val, data); + return this->priv_insert_commit(data, val); + } + + iterator insert_equal(const_iterator pos, BOOST_RV_REF(value_type) mval) + { + insert_commit_data data; + this->priv_insert_equal_prepare(pos, mval, data); + return this->priv_insert_commit(data, boost::move(mval)); + } + + template + void insert_unique(InIt first, InIt last) + { + for ( ; first != last; ++first){ + this->insert_unique(*first); + } + } + + template + void insert_equal(InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { this->priv_insert_equal_loop(first, last); } + + template + void insert_equal(InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + const size_type len = static_cast(std::distance(first, last)); + this->reserve(this->size()+len); + this->priv_insert_equal_loop(first, last); + } + + //Ordered + + template + void insert_equal(ordered_range_t, InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { this->priv_insert_equal_loop_ordered(first, last); } + + template + void insert_equal(ordered_range_t, FwdIt first, FwdIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_input_iterator::value && + container_detail::is_forward_iterator::value + >::type * = 0 + #endif + ) + { + const size_type len = static_cast(std::distance(first, last)); + this->reserve(this->size()+len); + this->priv_insert_equal_loop_ordered(first, last); + } + + template + void insert_equal(ordered_range_t, BidirIt first, BidirIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_input_iterator::value && + !container_detail::is_forward_iterator::value + >::type * = 0 + #endif + ) + { this->priv_insert_ordered_range(false, first, last); } + + template + void insert_unique(ordered_unique_range_t, InIt first, InIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < container_detail::is_input_iterator::value || + container_detail::is_forward_iterator::value + >::type * = 0 + #endif + ) + { + const_iterator pos(this->cend()); + for ( ; first != last; ++first){ + pos = this->insert_unique(pos, *first); + ++pos; + } + } + + template + void insert_unique(ordered_unique_range_t, BidirIt first, BidirIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !(container_detail::is_input_iterator::value || + container_detail::is_forward_iterator::value) + >::type * = 0 + #endif + ) + { this->priv_insert_ordered_range(true, first, last); } + + #ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + template + std::pair emplace_unique(Args&&... args) + { + aligned_storage::value> v; + value_type &val = *static_cast(static_cast(&v)); + stored_allocator_type &a = this->get_stored_allocator(); + stored_allocator_traits::construct(a, &val, ::boost::forward(args)... ); + value_destructor d(a, val); + return this->insert_unique(::boost::move(val)); + } + + template + iterator emplace_hint_unique(const_iterator hint, Args&&... args) + { + aligned_storage::value> v; + value_type &val = *static_cast(static_cast(&v)); + stored_allocator_type &a = this->get_stored_allocator(); + stored_allocator_traits::construct(a, &val, ::boost::forward(args)... ); + value_destructor d(a, val); + return this->insert_unique(hint, ::boost::move(val)); + } + + template + iterator emplace_equal(Args&&... args) + { + aligned_storage::value> v; + value_type &val = *static_cast(static_cast(&v)); + stored_allocator_type &a = this->get_stored_allocator(); + stored_allocator_traits::construct(a, &val, ::boost::forward(args)... ); + value_destructor d(a, val); + return this->insert_equal(::boost::move(val)); + } + + template + iterator emplace_hint_equal(const_iterator hint, Args&&... args) + { + aligned_storage::value> v; + value_type &val = *static_cast(static_cast(&v)); + stored_allocator_type &a = this->get_stored_allocator(); + stored_allocator_traits::construct(a, &val, ::boost::forward(args)... ); + value_destructor d(a, val); + return this->insert_equal(hint, ::boost::move(val)); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair \ + emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + aligned_storage::value> v; \ + value_type &val = *static_cast(static_cast(&v)); \ + stored_allocator_type &a = this->get_stored_allocator(); \ + stored_allocator_traits::construct(a, &val \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + value_destructor d(a, val); \ + return this->insert_unique(::boost::move(val)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint_unique(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + aligned_storage::value> v; \ + value_type &val = *static_cast(static_cast(&v)); \ + stored_allocator_type &a = this->get_stored_allocator(); \ + stored_allocator_traits::construct(a, &val \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + value_destructor d(a, val); \ + return this->insert_unique(hint, ::boost::move(val)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + aligned_storage::value> v; \ + value_type &val = *static_cast(static_cast(&v)); \ + stored_allocator_type &a = this->get_stored_allocator(); \ + stored_allocator_traits::construct(a, &val \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + value_destructor d(a, val); \ + return this->insert_equal(::boost::move(val)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint_equal(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + aligned_storage::value> v; \ + value_type &val = *static_cast(static_cast(&v)); \ + stored_allocator_type &a = this->get_stored_allocator(); \ + stored_allocator_traits::construct(a, &val \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + value_destructor d(a, val); \ + return this->insert_equal(hint, ::boost::move(val)); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + iterator erase(const_iterator position) + { return this->m_data.m_vect.erase(position); } + + size_type erase(const key_type& k) + { + std::pair itp = this->equal_range(k); + size_type ret = static_cast(itp.second-itp.first); + if (ret){ + this->m_data.m_vect.erase(itp.first, itp.second); + } + return ret; + } + + iterator erase(const_iterator first, const_iterator last) + { return this->m_data.m_vect.erase(first, last); } + + void clear() + { this->m_data.m_vect.clear(); } + + //! Effects: Tries to deallocate the excess of memory created + // with previous allocations. The size of the vector is unchanged + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { this->m_data.m_vect.shrink_to_fit(); } + + // set operations: + iterator find(const key_type& k) + { + iterator i = this->lower_bound(k); + iterator end_it = this->end(); + if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){ + i = end_it; + } + return i; + } + + const_iterator find(const key_type& k) const + { + const_iterator i = this->lower_bound(k); + + const_iterator end_it = this->cend(); + if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){ + i = end_it; + } + return i; + } + + // set operations: + size_type count(const key_type& k) const + { + std::pair p = this->equal_range(k); + size_type n = p.second - p.first; + return n; + } + + iterator lower_bound(const key_type& k) + { return this->priv_lower_bound(this->begin(), this->end(), k); } + + const_iterator lower_bound(const key_type& k) const + { return this->priv_lower_bound(this->cbegin(), this->cend(), k); } + + iterator upper_bound(const key_type& k) + { return this->priv_upper_bound(this->begin(), this->end(), k); } + + const_iterator upper_bound(const key_type& k) const + { return this->priv_upper_bound(this->cbegin(), this->cend(), k); } + + std::pair equal_range(const key_type& k) + { return this->priv_equal_range(this->begin(), this->end(), k); } + + std::pair equal_range(const key_type& k) const + { return this->priv_equal_range(this->cbegin(), this->cend(), k); } + + std::pair lower_bound_range(const key_type& k) + { return this->priv_lower_bound_range(this->begin(), this->end(), k); } + + std::pair lower_bound_range(const key_type& k) const + { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); } + + size_type capacity() const + { return this->m_data.m_vect.capacity(); } + + void reserve(size_type cnt) + { this->m_data.m_vect.reserve(cnt); } + + friend bool operator==(const flat_tree& x, const flat_tree& y) + { + return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); + } + + friend bool operator<(const flat_tree& x, const flat_tree& y) + { + return std::lexicographical_compare(x.begin(), x.end(), + y.begin(), y.end()); + } + + friend bool operator!=(const flat_tree& x, const flat_tree& y) + { return !(x == y); } + + friend bool operator>(const flat_tree& x, const flat_tree& y) + { return y < x; } + + friend bool operator<=(const flat_tree& x, const flat_tree& y) + { return !(y < x); } + + friend bool operator>=(const flat_tree& x, const flat_tree& y) + { return !(x < y); } + + friend void swap(flat_tree& x, flat_tree& y) + { x.swap(y); } + + private: + struct insert_commit_data + { + const_iterator position; + }; + + // insert/erase + void priv_insert_equal_prepare + (const_iterator pos, const value_type& val, insert_commit_data &data) + { + // N1780 + // To insert val at pos: + // if pos == end || val <= *pos + // if pos == begin || val >= *(pos-1) + // insert val before pos + // else + // insert val before upper_bound(val) + // else + // insert val before lower_bound(val) + const value_compare &val_cmp = this->m_data; + + if(pos == this->cend() || !val_cmp(*pos, val)){ + if (pos == this->cbegin() || !val_cmp(val, pos[-1])){ + data.position = pos; + } + else{ + data.position = + this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val)); + } + } + else{ + data.position = + this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val)); + } + } + + bool priv_insert_unique_prepare + (const_iterator b, const_iterator e, const value_type& val, insert_commit_data &commit_data) + { + const value_compare &val_cmp = this->m_data; + commit_data.position = this->priv_lower_bound(b, e, KeyOfValue()(val)); + return commit_data.position == e || val_cmp(val, *commit_data.position); + } + + bool priv_insert_unique_prepare + (const value_type& val, insert_commit_data &commit_data) + { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), val, commit_data); } + + bool priv_insert_unique_prepare + (const_iterator pos, const value_type& val, insert_commit_data &commit_data) + { + //N1780. Props to Howard Hinnant! + //To insert val at pos: + //if pos == end || val <= *pos + // if pos == begin || val >= *(pos-1) + // insert val before pos + // else + // insert val before upper_bound(val) + //else if pos+1 == end || val <= *(pos+1) + // insert val after pos + //else + // insert val before lower_bound(val) + const value_compare &val_cmp = this->m_data; + const const_iterator cend_it = this->cend(); + if(pos == cend_it || val_cmp(val, *pos)){ //Check if val should go before end + const const_iterator cbeg = this->cbegin(); + commit_data.position = pos; + if(pos == cbeg){ //If container is empty then insert it in the beginning + return true; + } + const_iterator prev(pos); + --prev; + if(val_cmp(*prev, val)){ //If previous element was less, then it should go between prev and pos + return true; + } + else if(!val_cmp(val, *prev)){ //If previous was equal then insertion should fail + commit_data.position = prev; + return false; + } + else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion + //but reduce the search between beg and prev as prev is bigger than val + return this->priv_insert_unique_prepare(cbeg, prev, val, commit_data); + } + } + else{ + //The hint is before the insertion position, so insert it + //in the remaining range [pos, end) + return this->priv_insert_unique_prepare(pos, cend_it, val, commit_data); + } + } + + template + iterator priv_insert_commit + (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible) + { + return this->m_data.m_vect.insert + ( commit_data.position + , boost::forward(convertible)); + } + + template + RanIt priv_lower_bound(RanIt first, const RanIt last, + const key_type & key) const + { + const Compare &key_cmp = this->m_data.get_comp(); + KeyOfValue key_extract; + size_type len = static_cast(last - first); + RanIt middle; + + while (len) { + size_type step = len >> 1; + middle = first; + middle += step; + + if (key_cmp(key_extract(*middle), key)) { + first = ++middle; + len -= step + 1; + } + else{ + len = step; + } + } + return first; + } + + template + RanIt priv_upper_bound(RanIt first, const RanIt last, + const key_type & key) const + { + const Compare &key_cmp = this->m_data.get_comp(); + KeyOfValue key_extract; + size_type len = static_cast(last - first); + RanIt middle; + + while (len) { + size_type step = len >> 1; + middle = first; + middle += step; + + if (key_cmp(key, key_extract(*middle))) { + len = step; + } + else{ + first = ++middle; + len -= step + 1; + } + } + return first; + } + + template + std::pair + priv_equal_range(RanIt first, RanIt last, const key_type& key) const + { + const Compare &key_cmp = this->m_data.get_comp(); + KeyOfValue key_extract; + size_type len = static_cast(last - first); + RanIt middle; + + while (len) { + size_type step = len >> 1; + middle = first; + middle += step; + + if (key_cmp(key_extract(*middle), key)){ + first = ++middle; + len -= step + 1; + } + else if (key_cmp(key, key_extract(*middle))){ + len = step; + } + else { + //Middle is equal to key + last = first; + last += len; + return std::pair + ( this->priv_lower_bound(first, middle, key) + , this->priv_upper_bound(++middle, last, key)); + } + } + return std::pair(first, first); + } + + template + std::pair priv_lower_bound_range(RanIt first, RanIt last, const key_type& k) const + { + const Compare &key_cmp = this->m_data.get_comp(); + KeyOfValue key_extract; + RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb); + if(lb != last && static_cast(!key_cmp(k, key_extract(*lb)))){ + ++ub; + } + return std::pair(lb, ub); + } + + template + void priv_insert_equal_loop(InIt first, InIt last) + { + for ( ; first != last; ++first){ + this->insert_equal(*first); + } + } + + template + void priv_insert_equal_loop_ordered(InIt first, InIt last) + { + const_iterator pos(this->cend()); + for ( ; first != last; ++first){ + //If ordered, then try hint version + //to achieve constant-time complexity per insertion + pos = this->insert_equal(pos, *first); + ++pos; + } + } + + template + void priv_insert_ordered_range(const bool unique_values, BidirIt first, BidirIt last) + { + size_type len = static_cast(std::distance(first, last)); + //Prereserve all memory so that iterators are not invalidated + this->reserve(this->size()+len); + //Auxiliary data for insertion positions. + const size_type BurstSize = len; + const ::boost::movelib::unique_ptr positions = + ::boost::movelib::make_unique_definit(BurstSize); + + const const_iterator b(this->cbegin()); + const const_iterator ce(this->cend()); + const_iterator pos(b); + const value_compare &val_cmp = this->m_data; + //Loop in burst sizes + bool back_insert = false; + while(len && !back_insert){ + const size_type burst = len < BurstSize ? len : BurstSize; + size_type unique_burst = 0u; + size_type checked = 0; + for(; checked != burst; ++checked){ + //Get the insertion position for each key, use std::iterator_traits::value_type + //because it can be different from container::value_type + //(e.g. conversion between std::pair -> boost::container::pair + const typename std::iterator_traits::value_type & val = *first; + pos = const_cast(*this).priv_lower_bound(pos, ce, KeyOfValue()(val)); + //Check if already present + if (pos != ce){ + ++first; + --len; + positions[checked] = (unique_values && !val_cmp(val, *pos)) ? + size_type(-1) : (++unique_burst, static_cast(pos - b)); + } + else{ //this element and the remaining should be back inserted + back_insert = true; + break; + } + } + if(unique_burst){ + //Insert all in a single step in the precalculated positions + this->m_data.m_vect.insert_ordered_at(unique_burst, positions.get() + checked, first); + //Next search position updated, iterator still valid because we've preserved the vector + pos += unique_burst; + } + } + //The remaining range should be back inserted + if(unique_values){ + while(len--){ + BidirIt next(first); + ++next; + if(next == last || val_cmp(*first, *next)){ + const bool room = this->m_data.m_vect.stable_emplace_back(*first); + (void)room; + BOOST_ASSERT(room); + } + first = next; + } + BOOST_ASSERT(first == last); + } + else{ + BOOST_ASSERT(size_type(std::distance(first, last)) == len); + if(len) + this->m_data.m_vect.insert(this->m_data.m_vect.cend(), len, first, last); + } + } +}; + +} //namespace container_detail { + +} //namespace container { +/* +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; +*/ +} //namespace boost { + +#include + +#endif // BOOST_CONTAINER_FLAT_TREE_HPP diff --git a/boost/container/detail/function_detector.hpp b/boost/container/detail/function_detector.hpp new file mode 100644 index 0000000..242eb41 --- /dev/null +++ b/boost/container/detail/function_detector.hpp @@ -0,0 +1,92 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2009-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +///////////////////////////////////////////////////////////////////////////// +// This code was modified from the code posted by Alexandre Courpron in his +// article "Interface Detection" in The Code Project: +// http://www.codeproject.com/KB/architecture/Detector.aspx +/////////////////////////////////////////////////////////////////////////////// +// Copyright 2007 Alexandre Courpron +// +// Permission to use, copy, modify, redistribute and sell this software, +// provided that this copyright notice appears on all copies of the software. +/////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_FUNCTION_DETECTOR_HPP +#define BOOST_CONTAINER_DETAIL_FUNCTION_DETECTOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace container { +namespace function_detector { + + typedef char NotFoundType; + struct StaticFunctionType { NotFoundType x [2]; }; + struct NonStaticFunctionType { NotFoundType x [3]; }; + + enum + { NotFound = 0, + StaticFunction = sizeof( StaticFunctionType ) - sizeof( NotFoundType ), + NonStaticFunction = sizeof( NonStaticFunctionType ) - sizeof( NotFoundType ) + }; + +} //namespace boost { +} //namespace container { +} //namespace function_detector { + +#define BOOST_CONTAINER_CREATE_FUNCTION_DETECTOR(Identifier, InstantiationKey) \ + namespace boost { \ + namespace container { \ + namespace function_detector { \ + template < class T, \ + class NonStaticType, \ + class NonStaticConstType, \ + class StaticType > \ + class DetectMember_##InstantiationKey_##Identifier { \ + template < NonStaticType > \ + struct TestNonStaticNonConst ; \ + \ + template < NonStaticConstType > \ + struct TestNonStaticConst ; \ + \ + template < StaticType > \ + struct TestStatic ; \ + \ + template \ + static NonStaticFunctionType Test( TestNonStaticNonConst<&U::Identifier>*, int ); \ + \ + template \ + static NonStaticFunctionType Test( TestNonStaticConst<&U::Identifier>*, int ); \ + \ + template \ + static StaticFunctionType Test( TestStatic<&U::Identifier>*, int ); \ + \ + template \ + static NotFoundType Test( ... ); \ + public : \ + static const int check = NotFound + (sizeof(Test(0, 0)) - sizeof(NotFoundType));\ + };\ +}}} //namespace boost::container::function_detector { + +#define BOOST_CONTAINER_DETECT_FUNCTION(Class, InstantiationKey, ReturnType, Identifier, Params) \ + ::boost::container::function_detector::DetectMember_##InstantiationKey_##Identifier< Class,\ + ReturnType (Class::*)Params,\ + ReturnType (Class::*)Params const,\ + ReturnType (*)Params \ + >::check + +#include + +#endif //@ifndef BOOST_CONTAINER_DETAIL_FUNCTION_DETECTOR_HPP diff --git a/boost/container/detail/hash_table.hpp b/boost/container/detail/hash_table.hpp new file mode 100644 index 0000000..da7bb53 --- /dev/null +++ b/boost/container/detail/hash_table.hpp @@ -0,0 +1,383 @@ +/* +template , class Pred = equal_to, + class Alloc = allocator > +class hash_set +{ +public: + // types + typedef Value key_type; + typedef key_type value_type; + typedef Hash hasher; + typedef Pred key_equal; + typedef Alloc allocator_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef typename allocator_traits::pointer pointer; + typedef typename allocator_traits::const_pointer const_pointer; + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::difference_type difference_type; + + typedef /unspecified/ iterator; + typedef /unspecified/ const_iterator; + typedef /unspecified/ local_iterator; + typedef /unspecified/ const_local_iterator; + + hash_set() + noexcept( + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value); + explicit hash_set(size_type n, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + template + hash_set(InputIterator f, InputIterator l, + size_type n = 0, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + explicit hash_set(const allocator_type&); + hash_set(const hash_set&); + hash_set(const hash_set&, const Allocator&); + hash_set(hash_set&&) + noexcept( + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value); + hash_set(hash_set&&, const Allocator&); + hash_set(initializer_list, size_type n = 0, + const hasher& hf = hasher(), const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + ~hash_set(); + hash_set& operator=(const hash_set&); + hash_set& operator=(hash_set&&) + noexcept( + allocator_type::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value); + hash_set& operator=(initializer_list); + + allocator_type get_allocator() const noexcept; + + bool empty() const noexcept; + size_type size() const noexcept; + size_type max_size() const noexcept; + + iterator begin() noexcept; + iterator end() noexcept; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + + template + pair emplace(Args&&... args); + template + iterator emplace_hint(const_iterator position, Args&&... args); + pair insert(const value_type& obj); + pair insert(value_type&& obj); + iterator insert(const_iterator hint, const value_type& obj); + iterator insert(const_iterator hint, value_type&& obj); + template + void insert(InputIterator first, InputIterator last); + void insert(initializer_list); + + iterator erase(const_iterator position); + size_type erase(const key_type& k); + iterator erase(const_iterator first, const_iterator last); + void clear() noexcept; + + void swap(hash_set&) + noexcept( + (!allocator_type::propagate_on_container_swap::value || + __is_nothrow_swappable::value) && + __is_nothrow_swappable::value && + __is_nothrow_swappable::value); + + hasher hash_function() const; + key_equal key_eq() const; + + iterator find(const key_type& k); + const_iterator find(const key_type& k) const; + size_type count(const key_type& k) const; + pair equal_range(const key_type& k); + pair equal_range(const key_type& k) const; + + size_type bucket_count() const noexcept; + size_type max_bucket_count() const noexcept; + + size_type bucket_size(size_type n) const; + size_type bucket(const key_type& k) const; + + local_iterator begin(size_type n); + local_iterator end(size_type n); + const_local_iterator begin(size_type n) const; + const_local_iterator end(size_type n) const; + const_local_iterator cbegin(size_type n) const; + const_local_iterator cend(size_type n) const; + + float load_factor() const noexcept; + float max_load_factor() const noexcept; + void max_load_factor(float z); + void rehash(size_type n); + void reserve(size_type n); +}; + +template , class Pred = equal_to, + class Alloc = allocator > > +class hash_map +{ +public: + // types + typedef Key key_type; + typedef T mapped_type; + typedef Hash hasher; + typedef Pred key_equal; + typedef Alloc allocator_type; + typedef pair value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef typename allocator_traits::pointer pointer; + typedef typename allocator_traits::const_pointer const_pointer; + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::difference_type difference_type; + + typedef /unspecified/ iterator; + typedef /unspecified/ const_iterator; + typedef /unspecified/ local_iterator; + typedef /unspecified/ const_local_iterator; + + hash_map() + noexcept( + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value); + explicit hash_map(size_type n, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + template + hash_map(InputIterator f, InputIterator l, + size_type n = 0, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + explicit hash_map(const allocator_type&); + hash_map(const hash_map&); + hash_map(const hash_map&, const Allocator&); + hash_map(hash_map&&) + noexcept( + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value); + hash_map(hash_map&&, const Allocator&); + hash_map(initializer_list, size_type n = 0, + const hasher& hf = hasher(), const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + ~hash_map(); + hash_map& operator=(const hash_map&); + hash_map& operator=(hash_map&&) + noexcept( + allocator_type::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value); + hash_map& operator=(initializer_list); + + allocator_type get_allocator() const noexcept; + + bool empty() const noexcept; + size_type size() const noexcept; + size_type max_size() const noexcept; + + iterator begin() noexcept; + iterator end() noexcept; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + + template + pair emplace(Args&&... args); + template + iterator emplace_hint(const_iterator position, Args&&... args); + pair insert(const value_type& obj); + template + pair insert(P&& obj); + iterator insert(const_iterator hint, const value_type& obj); + template + iterator insert(const_iterator hint, P&& obj); + template + void insert(InputIterator first, InputIterator last); + void insert(initializer_list); + + iterator erase(const_iterator position); + size_type erase(const key_type& k); + iterator erase(const_iterator first, const_iterator last); + void clear() noexcept; + + void swap(hash_map&) + noexcept( + (!allocator_type::propagate_on_container_swap::value || + __is_nothrow_swappable::value) && + __is_nothrow_swappable::value && + __is_nothrow_swappable::value); + + hasher hash_function() const; + key_equal key_eq() const; + + iterator find(const key_type& k); + const_iterator find(const key_type& k) const; + size_type count(const key_type& k) const; + pair equal_range(const key_type& k); + pair equal_range(const key_type& k) const; + + mapped_type& operator[](const key_type& k); + mapped_type& operator[](key_type&& k); + + mapped_type& at(const key_type& k); + const mapped_type& at(const key_type& k) const; + + size_type bucket_count() const noexcept; + size_type max_bucket_count() const noexcept; + + size_type bucket_size(size_type n) const; + size_type bucket(const key_type& k) const; + + local_iterator begin(size_type n); + local_iterator end(size_type n); + const_local_iterator begin(size_type n) const; + const_local_iterator end(size_type n) const; + const_local_iterator cbegin(size_type n) const; + const_local_iterator cend(size_type n) const; + + float load_factor() const noexcept; + float max_load_factor() const noexcept; + void max_load_factor(float z); + void rehash(size_type n); + void reserve(size_type n); +}; + +*/ + +template , class Pred = equal_to, + class Alloc = allocator > +class hash_table +{ +public: + // types + typedef Value key_type; + typedef key_type value_type; + typedef Hash hasher; + typedef Pred key_equal; + typedef Alloc allocator_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef typename allocator_traits::pointer pointer; + typedef typename allocator_traits::const_pointer const_pointer; + typedef typename allocator_traits::size_type size_type; + typedef typename allocator_traits::difference_type difference_type; + + typedef /unspecified/ iterator; + typedef /unspecified/ const_iterator; + typedef /unspecified/ local_iterator; + typedef /unspecified/ const_local_iterator; + + hash_set() + noexcept( + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value && + is_nothrow_default_constructible::value); + explicit hash_set(size_type n, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + template + hash_set(InputIterator f, InputIterator l, + size_type n = 0, const hasher& hf = hasher(), + const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + explicit hash_set(const allocator_type&); + hash_set(const hash_set&); + hash_set(const hash_set&, const Allocator&); + hash_set(hash_set&&) + noexcept( + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value && + is_nothrow_move_constructible::value); + hash_set(hash_set&&, const Allocator&); + hash_set(initializer_list, size_type n = 0, + const hasher& hf = hasher(), const key_equal& eql = key_equal(), + const allocator_type& a = allocator_type()); + ~hash_set(); + hash_set& operator=(const hash_set&); + hash_set& operator=(hash_set&&) + noexcept( + allocator_type::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value); + hash_set& operator=(initializer_list); + + allocator_type get_allocator() const noexcept; + + bool empty() const noexcept; + size_type size() const noexcept; + size_type max_size() const noexcept; + + iterator begin() noexcept; + iterator end() noexcept; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + + template + pair emplace(Args&&... args); + template + iterator emplace_hint(const_iterator position, Args&&... args); + pair insert(const value_type& obj); + pair insert(value_type&& obj); + iterator insert(const_iterator hint, const value_type& obj); + iterator insert(const_iterator hint, value_type&& obj); + template + void insert(InputIterator first, InputIterator last); + void insert(initializer_list); + + iterator erase(const_iterator position); + size_type erase(const key_type& k); + iterator erase(const_iterator first, const_iterator last); + void clear() noexcept; + + void swap(hash_set&) + noexcept( + (!allocator_type::propagate_on_container_swap::value || + __is_nothrow_swappable::value) && + __is_nothrow_swappable::value && + __is_nothrow_swappable::value); + + hasher hash_function() const; + key_equal key_eq() const; + + iterator find(const key_type& k); + const_iterator find(const key_type& k) const; + size_type count(const key_type& k) const; + pair equal_range(const key_type& k); + pair equal_range(const key_type& k) const; + + size_type bucket_count() const noexcept; + size_type max_bucket_count() const noexcept; + + size_type bucket_size(size_type n) const; + size_type bucket(const key_type& k) const; + + local_iterator begin(size_type n); + local_iterator end(size_type n); + const_local_iterator begin(size_type n) const; + const_local_iterator end(size_type n) const; + const_local_iterator cbegin(size_type n) const; + const_local_iterator cend(size_type n) const; + + float load_factor() const noexcept; + float max_load_factor() const noexcept; + void max_load_factor(float z); + void rehash(size_type n); + void reserve(size_type n); +}; diff --git a/boost/container/detail/iterators.hpp b/boost/container/detail/iterators.hpp new file mode 100644 index 0000000..0dabd3c --- /dev/null +++ b/boost/container/detail/iterators.hpp @@ -0,0 +1,821 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// (C) Copyright Gennaro Prota 2003 - 2004. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_ITERATORS_HPP +#define BOOST_CONTAINER_DETAIL_ITERATORS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_CONTAINER_PERFECT_FORWARDING +#include +#else +#include +#endif + +#include + +namespace boost { +namespace container { + +template +class constant_iterator + : public std::iterator + +{ + typedef constant_iterator this_type; + + public: + explicit constant_iterator(const T &ref, Difference range_size) + : m_ptr(&ref), m_num(range_size){} + + //Constructors + constant_iterator() + : m_ptr(0), m_num(0){} + + constant_iterator& operator++() + { increment(); return *this; } + + constant_iterator operator++(int) + { + constant_iterator result (*this); + increment(); + return result; + } + + constant_iterator& operator--() + { decrement(); return *this; } + + constant_iterator operator--(int) + { + constant_iterator result (*this); + decrement(); + return result; + } + + friend bool operator== (const constant_iterator& i, const constant_iterator& i2) + { return i.equal(i2); } + + friend bool operator!= (const constant_iterator& i, const constant_iterator& i2) + { return !(i == i2); } + + friend bool operator< (const constant_iterator& i, const constant_iterator& i2) + { return i.less(i2); } + + friend bool operator> (const constant_iterator& i, const constant_iterator& i2) + { return i2 < i; } + + friend bool operator<= (const constant_iterator& i, const constant_iterator& i2) + { return !(i > i2); } + + friend bool operator>= (const constant_iterator& i, const constant_iterator& i2) + { return !(i < i2); } + + friend Difference operator- (const constant_iterator& i, const constant_iterator& i2) + { return i2.distance_to(i); } + + //Arithmetic + constant_iterator& operator+=(Difference off) + { this->advance(off); return *this; } + + constant_iterator operator+(Difference off) const + { + constant_iterator other(*this); + other.advance(off); + return other; + } + + friend constant_iterator operator+(Difference off, const constant_iterator& right) + { return right + off; } + + constant_iterator& operator-=(Difference off) + { this->advance(-off); return *this; } + + constant_iterator operator-(Difference off) const + { return *this + (-off); } + + const T& operator*() const + { return dereference(); } + + const T& operator[] (Difference ) const + { return dereference(); } + + const T* operator->() const + { return &(dereference()); } + + private: + const T * m_ptr; + Difference m_num; + + void increment() + { --m_num; } + + void decrement() + { ++m_num; } + + bool equal(const this_type &other) const + { return m_num == other.m_num; } + + bool less(const this_type &other) const + { return other.m_num < m_num; } + + const T & dereference() const + { return *m_ptr; } + + void advance(Difference n) + { m_num -= n; } + + Difference distance_to(const this_type &other)const + { return m_num - other.m_num; } +}; + +template +class value_init_construct_iterator + : public std::iterator + +{ + typedef value_init_construct_iterator this_type; + + public: + explicit value_init_construct_iterator(Difference range_size) + : m_num(range_size){} + + //Constructors + value_init_construct_iterator() + : m_num(0){} + + value_init_construct_iterator& operator++() + { increment(); return *this; } + + value_init_construct_iterator operator++(int) + { + value_init_construct_iterator result (*this); + increment(); + return result; + } + + value_init_construct_iterator& operator--() + { decrement(); return *this; } + + value_init_construct_iterator operator--(int) + { + value_init_construct_iterator result (*this); + decrement(); + return result; + } + + friend bool operator== (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return i.equal(i2); } + + friend bool operator!= (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return !(i == i2); } + + friend bool operator< (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return i.less(i2); } + + friend bool operator> (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return i2 < i; } + + friend bool operator<= (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return !(i > i2); } + + friend bool operator>= (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return !(i < i2); } + + friend Difference operator- (const value_init_construct_iterator& i, const value_init_construct_iterator& i2) + { return i2.distance_to(i); } + + //Arithmetic + value_init_construct_iterator& operator+=(Difference off) + { this->advance(off); return *this; } + + value_init_construct_iterator operator+(Difference off) const + { + value_init_construct_iterator other(*this); + other.advance(off); + return other; + } + + friend value_init_construct_iterator operator+(Difference off, const value_init_construct_iterator& right) + { return right + off; } + + value_init_construct_iterator& operator-=(Difference off) + { this->advance(-off); return *this; } + + value_init_construct_iterator operator-(Difference off) const + { return *this + (-off); } + + //This pseudo-iterator's dereference operations have no sense since value is not + //constructed until ::boost::container::construct_in_place is called. + //So comment them to catch bad uses + //const T& operator*() const; + //const T& operator[](difference_type) const; + //const T* operator->() const; + + private: + Difference m_num; + + void increment() + { --m_num; } + + void decrement() + { ++m_num; } + + bool equal(const this_type &other) const + { return m_num == other.m_num; } + + bool less(const this_type &other) const + { return other.m_num < m_num; } + + const T & dereference() const + { + static T dummy; + return dummy; + } + + void advance(Difference n) + { m_num -= n; } + + Difference distance_to(const this_type &other)const + { return m_num - other.m_num; } +}; + +template +class default_init_construct_iterator + : public std::iterator + +{ + typedef default_init_construct_iterator this_type; + + public: + explicit default_init_construct_iterator(Difference range_size) + : m_num(range_size){} + + //Constructors + default_init_construct_iterator() + : m_num(0){} + + default_init_construct_iterator& operator++() + { increment(); return *this; } + + default_init_construct_iterator operator++(int) + { + default_init_construct_iterator result (*this); + increment(); + return result; + } + + default_init_construct_iterator& operator--() + { decrement(); return *this; } + + default_init_construct_iterator operator--(int) + { + default_init_construct_iterator result (*this); + decrement(); + return result; + } + + friend bool operator== (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return i.equal(i2); } + + friend bool operator!= (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return !(i == i2); } + + friend bool operator< (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return i.less(i2); } + + friend bool operator> (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return i2 < i; } + + friend bool operator<= (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return !(i > i2); } + + friend bool operator>= (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return !(i < i2); } + + friend Difference operator- (const default_init_construct_iterator& i, const default_init_construct_iterator& i2) + { return i2.distance_to(i); } + + //Arithmetic + default_init_construct_iterator& operator+=(Difference off) + { this->advance(off); return *this; } + + default_init_construct_iterator operator+(Difference off) const + { + default_init_construct_iterator other(*this); + other.advance(off); + return other; + } + + friend default_init_construct_iterator operator+(Difference off, const default_init_construct_iterator& right) + { return right + off; } + + default_init_construct_iterator& operator-=(Difference off) + { this->advance(-off); return *this; } + + default_init_construct_iterator operator-(Difference off) const + { return *this + (-off); } + + //This pseudo-iterator's dereference operations have no sense since value is not + //constructed until ::boost::container::construct_in_place is called. + //So comment them to catch bad uses + //const T& operator*() const; + //const T& operator[](difference_type) const; + //const T* operator->() const; + + private: + Difference m_num; + + void increment() + { --m_num; } + + void decrement() + { ++m_num; } + + bool equal(const this_type &other) const + { return m_num == other.m_num; } + + bool less(const this_type &other) const + { return other.m_num < m_num; } + + const T & dereference() const + { + static T dummy; + return dummy; + } + + void advance(Difference n) + { m_num -= n; } + + Difference distance_to(const this_type &other)const + { return m_num - other.m_num; } +}; + + +template +class repeat_iterator + : public std::iterator + +{ + typedef repeat_iterator this_type; + public: + explicit repeat_iterator(T &ref, Difference range_size) + : m_ptr(&ref), m_num(range_size){} + + //Constructors + repeat_iterator() + : m_ptr(0), m_num(0){} + + this_type& operator++() + { increment(); return *this; } + + this_type operator++(int) + { + this_type result (*this); + increment(); + return result; + } + + this_type& operator--() + { increment(); return *this; } + + this_type operator--(int) + { + this_type result (*this); + increment(); + return result; + } + + friend bool operator== (const this_type& i, const this_type& i2) + { return i.equal(i2); } + + friend bool operator!= (const this_type& i, const this_type& i2) + { return !(i == i2); } + + friend bool operator< (const this_type& i, const this_type& i2) + { return i.less(i2); } + + friend bool operator> (const this_type& i, const this_type& i2) + { return i2 < i; } + + friend bool operator<= (const this_type& i, const this_type& i2) + { return !(i > i2); } + + friend bool operator>= (const this_type& i, const this_type& i2) + { return !(i < i2); } + + friend Difference operator- (const this_type& i, const this_type& i2) + { return i2.distance_to(i); } + + //Arithmetic + this_type& operator+=(Difference off) + { this->advance(off); return *this; } + + this_type operator+(Difference off) const + { + this_type other(*this); + other.advance(off); + return other; + } + + friend this_type operator+(Difference off, const this_type& right) + { return right + off; } + + this_type& operator-=(Difference off) + { this->advance(-off); return *this; } + + this_type operator-(Difference off) const + { return *this + (-off); } + + T& operator*() const + { return dereference(); } + + T& operator[] (Difference ) const + { return dereference(); } + + T *operator->() const + { return &(dereference()); } + + private: + T * m_ptr; + Difference m_num; + + void increment() + { --m_num; } + + void decrement() + { ++m_num; } + + bool equal(const this_type &other) const + { return m_num == other.m_num; } + + bool less(const this_type &other) const + { return other.m_num < m_num; } + + T & dereference() const + { return *m_ptr; } + + void advance(Difference n) + { m_num -= n; } + + Difference distance_to(const this_type &other)const + { return m_num - other.m_num; } +}; + +template +class emplace_iterator + : public std::iterator + +{ + typedef emplace_iterator this_type; + + public: + typedef Difference difference_type; + explicit emplace_iterator(EmplaceFunctor&e) + : m_num(1), m_pe(&e){} + + emplace_iterator() + : m_num(0), m_pe(0){} + + this_type& operator++() + { increment(); return *this; } + + this_type operator++(int) + { + this_type result (*this); + increment(); + return result; + } + + this_type& operator--() + { decrement(); return *this; } + + this_type operator--(int) + { + this_type result (*this); + decrement(); + return result; + } + + friend bool operator== (const this_type& i, const this_type& i2) + { return i.equal(i2); } + + friend bool operator!= (const this_type& i, const this_type& i2) + { return !(i == i2); } + + friend bool operator< (const this_type& i, const this_type& i2) + { return i.less(i2); } + + friend bool operator> (const this_type& i, const this_type& i2) + { return i2 < i; } + + friend bool operator<= (const this_type& i, const this_type& i2) + { return !(i > i2); } + + friend bool operator>= (const this_type& i, const this_type& i2) + { return !(i < i2); } + + friend difference_type operator- (const this_type& i, const this_type& i2) + { return i2.distance_to(i); } + + //Arithmetic + this_type& operator+=(difference_type off) + { this->advance(off); return *this; } + + this_type operator+(difference_type off) const + { + this_type other(*this); + other.advance(off); + return other; + } + + friend this_type operator+(difference_type off, const this_type& right) + { return right + off; } + + this_type& operator-=(difference_type off) + { this->advance(-off); return *this; } + + this_type operator-(difference_type off) const + { return *this + (-off); } + + //This pseudo-iterator's dereference operations have no sense since value is not + //constructed until ::boost::container::construct_in_place is called. + //So comment them to catch bad uses + //const T& operator*() const; + //const T& operator[](difference_type) const; + //const T* operator->() const; + + template + void construct_in_place(A &a, T* ptr) + { (*m_pe)(a, ptr); } + + private: + difference_type m_num; + EmplaceFunctor * m_pe; + + void increment() + { --m_num; } + + void decrement() + { ++m_num; } + + bool equal(const this_type &other) const + { return m_num == other.m_num; } + + bool less(const this_type &other) const + { return other.m_num < m_num; } + + const T & dereference() const + { + static T dummy; + return dummy; + } + + void advance(difference_type n) + { m_num -= n; } + + difference_type distance_to(const this_type &other)const + { return difference_type(m_num - other.m_num); } +}; + +#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + +template +struct emplace_functor +{ + typedef typename container_detail::build_number_seq::type index_tuple_t; + + emplace_functor(Args&&... args) + : args_(args...) + {} + + template + void operator()(A &a, T *ptr) + { emplace_functor::inplace_impl(a, ptr, index_tuple_t()); } + + template + void inplace_impl(A &a, T* ptr, const container_detail::index_tuple&) + { + allocator_traits::construct + (a, ptr, ::boost::forward(container_detail::get(args_))...); + } + + container_detail::tuple args_; +}; + +#else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + +#define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template <) \ + BOOST_PP_ENUM_PARAMS(n, class P) \ + BOOST_PP_EXPR_IF(n, >) \ + struct BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \ + { \ + BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \ + ( BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \ + BOOST_PP_EXPR_IF(n, :) BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_INIT, _){} \ + \ + template \ + void operator()(A &a, T *ptr) \ + { \ + allocator_traits::construct \ + (a, ptr BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) ); \ + } \ + BOOST_PP_REPEAT(n, BOOST_CONTAINER_PP_PARAM_DEFINE, _) \ + }; \ + //! +#define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) +#include BOOST_PP_LOCAL_ITERATE() + +#endif + +namespace container_detail { + +template +struct has_iterator_category +{ + template + static char test(int, typename X::iterator_category*); + + template + static int test(int, ...); + + static const bool value = (1 == sizeof(test(0, 0))); +}; + + +template::value > +struct is_input_iterator +{ + static const bool value = is_same::value; +}; + +template +struct is_input_iterator +{ + static const bool value = false; +}; + +template::value > +struct is_forward_iterator +{ + static const bool value = is_same::value; +}; + +template +struct is_forward_iterator +{ + static const bool value = false; +}; + +template::value > +struct is_bidirectional_iterator +{ + static const bool value = is_same::value; +}; + +template +struct is_bidirectional_iterator +{ + static const bool value = false; +}; + +template +struct iiterator_node_value_type { + typedef typename IINodeType::value_type type; +}; + +template +struct iiterator_types +{ + typedef typename IIterator::value_type it_value_type; + typedef typename iiterator_node_value_type::type value_type; + typedef typename std::iterator_traits::pointer it_pointer; + typedef typename std::iterator_traits::difference_type difference_type; + typedef typename ::boost::intrusive::pointer_traits:: + template rebind_pointer::type pointer; + typedef typename ::boost::intrusive::pointer_traits:: + template rebind_pointer::type const_pointer; + typedef typename ::boost::intrusive:: + pointer_traits::reference reference; + typedef typename ::boost::intrusive:: + pointer_traits::reference const_reference; + typedef typename IIterator::iterator_category iterator_category; +}; + +template +struct std_iterator +{ + typedef typename std::iterator + < typename iiterator_types::iterator_category + , typename iiterator_types::value_type + , typename iiterator_types::difference_type + , typename iiterator_types::const_pointer + , typename iiterator_types::const_reference> type; +}; + +template +struct std_iterator +{ + typedef typename std::iterator + < typename iiterator_types::iterator_category + , typename iiterator_types::value_type + , typename iiterator_types::difference_type + , typename iiterator_types::pointer + , typename iiterator_types::reference> type; +}; + +template +class iterator +{ + typedef typename std_iterator::type types_t; + + public: + typedef typename types_t::pointer pointer; + typedef typename types_t::reference reference; + typedef typename types_t::difference_type difference_type; + typedef typename types_t::iterator_category iterator_category; + typedef typename types_t::value_type value_type; + + iterator() + {} + + explicit iterator(IIterator iit) BOOST_CONTAINER_NOEXCEPT + : m_iit(iit) + {} + + iterator(iterator const& other) BOOST_CONTAINER_NOEXCEPT + : m_iit(other.get()) + {} + + iterator& operator++() BOOST_CONTAINER_NOEXCEPT + { ++this->m_iit; return *this; } + + iterator operator++(int) BOOST_CONTAINER_NOEXCEPT + { + iterator result (*this); + ++this->m_iit; + return result; + } + + iterator& operator--() BOOST_CONTAINER_NOEXCEPT + { + //If the iterator is not a bidirectional iterator, operator-- should not exist + BOOST_STATIC_ASSERT((is_bidirectional_iterator::value)); + --this->m_iit; return *this; + } + + iterator operator--(int) BOOST_CONTAINER_NOEXCEPT + { + iterator result (*this); + --this->m_iit; + return result; + } + + friend bool operator== (const iterator& l, const iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_iit == r.m_iit; } + + friend bool operator!= (const iterator& l, const iterator& r) BOOST_CONTAINER_NOEXCEPT + { return !(l == r); } + + reference operator*() const BOOST_CONTAINER_NOEXCEPT + { return (*this->m_iit).get_data(); } + + pointer operator->() const BOOST_CONTAINER_NOEXCEPT + { return ::boost::intrusive::pointer_traits::pointer_to(this->operator*()); } + + const IIterator &get() const BOOST_CONTAINER_NOEXCEPT + { return this->m_iit; } + + private: + IIterator m_iit; +}; + +using ::boost::intrusive::detail::reverse_iterator; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ITERATORS_HPP diff --git a/boost/container/detail/math_functions.hpp b/boost/container/detail/math_functions.hpp new file mode 100644 index 0000000..6853b9c --- /dev/null +++ b/boost/container/detail/math_functions.hpp @@ -0,0 +1,119 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Stephen Cleary 2000. +// (C) Copyright Ion Gaztanaga 2007-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +// This file is a slightly modified file from Boost.Pool +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP +#define BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +// Greatest common divisor and least common multiple + +// +// gcd is an algorithm that calculates the greatest common divisor of two +// integers, using Euclid's algorithm. +// +// Pre: A > 0 && B > 0 +// Recommended: A > B +template +inline Integer gcd(Integer A, Integer B) +{ + do + { + const Integer tmp(B); + B = A % B; + A = tmp; + } while (B != 0); + + return A; +} + +// +// lcm is an algorithm that calculates the least common multiple of two +// integers. +// +// Pre: A > 0 && B > 0 +// Recommended: A > B +template +inline Integer lcm(const Integer & A, const Integer & B) +{ + Integer ret = A; + ret /= gcd(A, B); + ret *= B; + return ret; +} + +template +inline Integer log2_ceil(const Integer & A) +{ + Integer i = 0; + Integer power_of_2 = 1; + + while(power_of_2 < A){ + power_of_2 <<= 1; + ++i; + } + return i; +} + +template +inline Integer upper_power_of_2(const Integer & A) +{ + Integer power_of_2 = 1; + + while(power_of_2 < A){ + power_of_2 <<= 1; + } + return power_of_2; +} + +//This function uses binary search to discover the +//highest set bit of the integer +inline std::size_t floor_log2 (std::size_t x) +{ + const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT; + const bool Size_t_Bits_Power_2= !(Bits & (Bits-1)); + BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true)); + + std::size_t n = x; + std::size_t log2 = 0; + + for(std::size_t shift = Bits >> 1; shift; shift >>= 1){ + std::size_t tmp = n >> shift; + if (tmp) + log2 += shift, n = tmp; + } + + return log2; +} + +} // namespace container_detail +} // namespace container +} // namespace boost + +#include + +#endif diff --git a/boost/container/detail/memory_util.hpp b/boost/container/detail/memory_util.hpp new file mode 100644 index 0000000..7f055cb --- /dev/null +++ b/boost/container/detail/memory_util.hpp @@ -0,0 +1,90 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATOR_MEMORY_UTIL_HPP +#define BOOST_CONTAINER_ALLOCATOR_MEMORY_UTIL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include + + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME allocate +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (2, 2, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME destroy +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME max_size +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 0, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME select_on_container_copy_construction +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 0, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME construct +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#ifdef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_SINGLE_ITERATION +# define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#else +# define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS+1, )) +#endif +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME swap +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace container { namespace container_detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +namespace boost { +namespace container { +namespace container_detail { + +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(pointer) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(const_pointer) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(reference) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(const_reference) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(void_pointer) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(const_void_pointer) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(size_type) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(propagate_on_container_copy_assignment) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(propagate_on_container_move_assignment) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(propagate_on_container_swap) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(difference_type) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(value_compare) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(wrapped_value_compare) + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif // ! defined(BOOST_CONTAINER_ALLOCATOR_MEMORY_UTIL_HPP) diff --git a/boost/container/detail/mpl.hpp b/boost/container/detail/mpl.hpp new file mode 100644 index 0000000..ceac52a --- /dev/null +++ b/boost/container/detail/mpl.hpp @@ -0,0 +1,183 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_MPL_HPP +#define BOOST_CONTAINER_CONTAINER_DETAIL_MPL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +struct integral_constant +{ + static const T value = val; + typedef integral_constant type; +}; + +template< bool C_ > +struct bool_ : integral_constant +{ + static const bool value = C_; + operator bool() const { return bool_::value; } +}; + +typedef bool_ true_; +typedef bool_ false_; + +typedef true_ true_type; +typedef false_ false_type; + +typedef char yes_type; +struct no_type +{ + char padding[8]; +}; + +template +struct enable_if_c { + typedef T type; +}; + +template +struct enable_if_c {}; + +template +struct enable_if : public enable_if_c {}; + +template +struct disable_if : public enable_if_c {}; + +template +struct disable_if_c : public enable_if_c {}; + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) + +template +struct is_convertible +{ + static const bool value = __is_convertible_to(T, U); +}; + +#else + +template +class is_convertible +{ + typedef char true_t; + class false_t { char dummy[2]; }; + //use any_conversion as first parameter since in MSVC + //overaligned types can't go through ellipsis + static false_t dispatch(...); + static true_t dispatch(U); + static T &trigger(); + public: + static const bool value = sizeof(dispatch(trigger())) == sizeof(true_t); +}; + +#endif + +template< + bool C + , typename T1 + , typename T2 + > +struct if_c +{ + typedef T1 type; +}; + +template< + typename T1 + , typename T2 + > +struct if_c +{ + typedef T2 type; +}; + +template< + typename T1 + , typename T2 + , typename T3 + > +struct if_ +{ + typedef typename if_c<0 != T1::value, T2, T3>::type type; +}; + + +template +struct select1st +{ + typedef Pair argument_type; + typedef typename Pair::first_type result_type; + + template + const typename Pair::first_type& operator()(const OtherPair& x) const + { return x.first; } + + const typename Pair::first_type& operator()(const typename Pair::first_type& x) const + { return x; } +}; + +// identity is an extension: it is not part of the standard. +template +struct identity +{ + typedef T argument_type; + typedef T result_type; + + typedef T type; + const T& operator()(const T& x) const + { return x; } +}; + +template +struct ls_zeros +{ + static const std::size_t value = (S & std::size_t(1)) ? 0 : (1u + ls_zeros<(S >> 1u)>::value); +}; + +template<> +struct ls_zeros<0> +{ + static const std::size_t value = 0; +}; + +template<> +struct ls_zeros<1> +{ + static const std::size_t value = 0; +}; + +template struct unvoid { typedef T type; }; +template <> struct unvoid { struct type { }; }; +template <> struct unvoid { struct type { }; }; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_MPL_HPP + diff --git a/boost/container/detail/multiallocation_chain.hpp b/boost/container/detail/multiallocation_chain.hpp new file mode 100644 index 0000000..96f6202 --- /dev/null +++ b/boost/container/detail/multiallocation_chain.hpp @@ -0,0 +1,292 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP +#define BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +class basic_multiallocation_chain +{ + private: + typedef bi::slist_base_hook + ,bi::link_mode + > node; + + typedef typename boost::intrusive::pointer_traits + ::template rebind_pointer::type char_ptr; + typedef typename boost::intrusive:: + pointer_traits::difference_type difference_type; + + typedef bi::slist< node + , bi::linear + , bi::cache_last + , bi::size_type::type> + > slist_impl_t; + slist_impl_t slist_impl_; + + typedef typename boost::intrusive::pointer_traits + ::template rebind_pointer::type node_ptr; + typedef typename boost::intrusive:: + pointer_traits node_ptr_traits; + + static node & to_node(const VoidPointer &p) + { return *static_cast(static_cast(container_detail::to_raw_pointer(p))); } + + static VoidPointer from_node(node &n) + { return node_ptr_traits::pointer_to(n); } + + static node_ptr to_node_ptr(const VoidPointer &p) + { return node_ptr_traits::static_cast_from(p); } + + BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_multiallocation_chain) + + public: + + typedef VoidPointer void_pointer; + typedef typename slist_impl_t::iterator iterator; + typedef typename slist_impl_t::size_type size_type; + + basic_multiallocation_chain() + : slist_impl_() + {} + + basic_multiallocation_chain(const void_pointer &b, const void_pointer &before_e, size_type n) + : slist_impl_(to_node_ptr(b), to_node_ptr(before_e), n) + {} + + basic_multiallocation_chain(BOOST_RV_REF(basic_multiallocation_chain) other) + : slist_impl_(::boost::move(other.slist_impl_)) + {} + + basic_multiallocation_chain& operator=(BOOST_RV_REF(basic_multiallocation_chain) other) + { + slist_impl_ = ::boost::move(other.slist_impl_); + return *this; + } + + bool empty() const + { return slist_impl_.empty(); } + + size_type size() const + { return slist_impl_.size(); } + + iterator before_begin() + { return slist_impl_.before_begin(); } + + iterator begin() + { return slist_impl_.begin(); } + + iterator end() + { return slist_impl_.end(); } + + iterator last() + { return slist_impl_.last(); } + + void clear() + { slist_impl_.clear(); } + + iterator insert_after(iterator it, void_pointer m) + { return slist_impl_.insert_after(it, to_node(m)); } + + void push_front(const void_pointer &m) + { return slist_impl_.push_front(to_node(m)); } + + void push_back(const void_pointer &m) + { return slist_impl_.push_back(to_node(m)); } + + void_pointer pop_front() + { + node & n = slist_impl_.front(); + void_pointer ret = from_node(n); + slist_impl_.pop_front(); + return ret; + } + + void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n) + { slist_impl_.splice_after(after_this, x.slist_impl_, before_b, before_e, n); } + + void splice_after(iterator after_this, basic_multiallocation_chain &x) + { slist_impl_.splice_after(after_this, x.slist_impl_); } + + void erase_after(iterator before_b, iterator e, size_type n) + { slist_impl_.erase_after(before_b, e, n); } + + void_pointer incorporate_after(iterator after_this, const void_pointer &b, size_type unit_bytes, size_type num_units) + { + typedef typename boost::intrusive::pointer_traits char_pointer_traits; + char_ptr elem = char_pointer_traits::static_cast_from(b); + if(num_units){ + char_ptr prev_elem = elem; + elem += unit_bytes; + for(size_type i = 0; i != num_units-1; ++i, elem += unit_bytes){ + ::new (container_detail::to_raw_pointer(prev_elem)) void_pointer(elem); + prev_elem = elem; + } + slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(prev_elem), num_units); + } + return elem; + } + + void incorporate_after(iterator after_this, void_pointer b, void_pointer before_e, size_type n) + { slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(before_e), n); } + + void swap(basic_multiallocation_chain &x) + { slist_impl_.swap(x.slist_impl_); } + + static iterator iterator_to(const void_pointer &p) + { return slist_impl_t::s_iterator_to(to_node(p)); } + + std::pair extract_data() + { + std::pair ret + (slist_impl_.begin().operator->() + ,slist_impl_.last().operator->()); + slist_impl_.clear(); + return ret; + } +}; + +template +struct cast_functor +{ + typedef typename container_detail::add_reference::type result_type; + template + result_type operator()(U &ptr) const + { return *static_cast(static_cast(&ptr)); } +}; + +template +class transform_multiallocation_chain + : public MultiallocationChain +{ + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(transform_multiallocation_chain) + //transform_multiallocation_chain(const transform_multiallocation_chain &); + //transform_multiallocation_chain & operator=(const transform_multiallocation_chain &); + + typedef typename MultiallocationChain::void_pointer void_pointer; + typedef typename boost::intrusive::pointer_traits + void_pointer_traits; + typedef typename void_pointer_traits::template + rebind_pointer::type pointer; + typedef typename boost::intrusive::pointer_traits + pointer_traits; + + static pointer cast(const void_pointer &p) + { return pointer_traits::static_cast_from(p); } + + public: + typedef transform_iterator + < typename MultiallocationChain::iterator + , container_detail::cast_functor > iterator; + typedef typename MultiallocationChain::size_type size_type; + + transform_multiallocation_chain() + : MultiallocationChain() + {} + + transform_multiallocation_chain(BOOST_RV_REF(transform_multiallocation_chain) other) + : MultiallocationChain(::boost::move(static_cast(other))) + {} + + transform_multiallocation_chain(BOOST_RV_REF(MultiallocationChain) other) + : MultiallocationChain(::boost::move(static_cast(other))) + {} + + transform_multiallocation_chain& operator=(BOOST_RV_REF(transform_multiallocation_chain) other) + { + return static_cast + (this->MultiallocationChain::operator=(::boost::move(static_cast(other)))); + } +/* + void push_front(const pointer &mem) + { holder_.push_front(mem); } + + void push_back(const pointer &mem) + { return holder_.push_back(mem); } + + void swap(transform_multiallocation_chain &other_chain) + { holder_.swap(other_chain.holder_); } + + void splice_after(iterator after_this, transform_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n) + { holder_.splice_after(after_this.base(), x.holder_, before_b.base(), before_e.base(), n); } + + void incorporate_after(iterator after_this, pointer b, pointer before_e, size_type n) + { holder_.incorporate_after(after_this.base(), b, before_e, n); } +*/ + pointer pop_front() + { return cast(this->MultiallocationChain::pop_front()); } +/* + bool empty() const + { return holder_.empty(); } + + iterator before_begin() + { return iterator(holder_.before_begin()); } +*/ + iterator begin() + { return iterator(this->MultiallocationChain::begin()); } +/* + iterator end() + { return iterator(holder_.end()); } + + iterator last() + { return iterator(holder_.last()); } + + size_type size() const + { return holder_.size(); } + + void clear() + { holder_.clear(); } +*/ + iterator insert_after(iterator it, pointer m) + { return iterator(this->MultiallocationChain::insert_after(it.base(), m)); } + + static iterator iterator_to(const pointer &p) + { return iterator(MultiallocationChain::iterator_to(p)); } + + std::pair extract_data() + { + std::pair data(this->MultiallocationChain::extract_data()); + return std::pair(cast(data.first), cast(data.second)); + } +/* + MultiallocationChain &extract_multiallocation_chain() + { return holder_; }*/ +}; + +}}} + +// namespace container_detail { +// namespace container { +// namespace boost { + +#include + +#endif //BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP diff --git a/boost/container/detail/mutex.hpp b/boost/container/detail/mutex.hpp new file mode 100644 index 0000000..c53afa1 --- /dev/null +++ b/boost/container/detail/mutex.hpp @@ -0,0 +1,284 @@ +// Copyright (C) 2000 Stephen Cleary +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org for updates, documentation, and revision history. + +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_MUTEX_HPP +#define BOOST_CONTAINER_MUTEX_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +//#define BOOST_CONTAINER_NO_MT +//#define BOOST_CONTAINER_NO_SPINLOCKS + +#include +#include + +// Extremely Light-Weight wrapper classes for OS thread synchronization + +#define BOOST_MUTEX_HELPER_NONE 0 +#define BOOST_MUTEX_HELPER_WIN32 1 +#define BOOST_MUTEX_HELPER_PTHREAD 2 +#define BOOST_MUTEX_HELPER_SPINLOCKS 3 + +#if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT) +# define BOOST_NO_MT +#endif + +#if defined(BOOST_NO_MT) || defined(BOOST_CONTAINER_NO_MT) + // No multithreading -> make locks into no-ops + #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE +#else + //Taken from dlmalloc + #if !defined(BOOST_CONTAINER_NO_SPINLOCKS) && \ + ((defined(__GNUC__) && \ + ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \ + defined(__i386__) || defined(__x86_64__))) || \ + (defined(_MSC_VER) && _MSC_VER>=1310)) + #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_SPINLOCKS + #endif + + #if defined(BOOST_WINDOWS) + #include + #ifndef BOOST_MUTEX_HELPER + #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32 + #endif + #elif defined(BOOST_HAS_UNISTD_H) + #include + #if !defined(BOOST_MUTEX_HELPER) && (defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)) + #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD + #endif + #endif +#endif + +#ifndef BOOST_MUTEX_HELPER + #error Unable to determine platform mutex type; #define BOOST_NO_MT to assume single-threaded +#endif + +#if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE + //... +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS + #if defined(_MSC_VER) + #ifndef _M_AMD64 + /* These are already defined on AMD64 builds */ + #ifdef __cplusplus + extern "C" { + #endif /* __cplusplus */ + long __cdecl _InterlockedCompareExchange(long volatile *Dest, long Exchange, long Comp); + long __cdecl _InterlockedExchange(long volatile *Target, long Value); + #ifdef __cplusplus + } + #endif /* __cplusplus */ + #endif /* _M_AMD64 */ + #pragma intrinsic (_InterlockedCompareExchange) + #pragma intrinsic (_InterlockedExchange) + #define interlockedcompareexchange _InterlockedCompareExchange + #define interlockedexchange _InterlockedExchange + #elif defined(WIN32) && defined(__GNUC__) + #define interlockedcompareexchange(a, b, c) __sync_val_compare_and_swap(a, c, b) + #define interlockedexchange __sync_lock_test_and_set + #endif /* Win32 */ + + /* First, define CAS_LOCK and CLEAR_LOCK on ints */ + /* Note CAS_LOCK defined to return 0 on success */ + + #if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) + #define BOOST_CONTAINER_CAS_LOCK(sl) __sync_lock_test_and_set(sl, 1) + #define BOOST_CONTAINER_CLEAR_LOCK(sl) __sync_lock_release(sl) + + #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) + /* Custom spin locks for older gcc on x86 */ + static FORCEINLINE int boost_container_x86_cas_lock(int *sl) { + int ret; + int val = 1; + int cmp = 0; + __asm__ __volatile__ ("lock; cmpxchgl %1, %2" + : "=a" (ret) + : "r" (val), "m" (*(sl)), "0"(cmp) + : "memory", "cc"); + return ret; + } + + static FORCEINLINE void boost_container_x86_clear_lock(int* sl) { + assert(*sl != 0); + int prev = 0; + int ret; + __asm__ __volatile__ ("lock; xchgl %0, %1" + : "=r" (ret) + : "m" (*(sl)), "0"(prev) + : "memory"); + } + + #define BOOST_CONTAINER_CAS_LOCK(sl) boost_container_x86_cas_lock(sl) + #define BOOST_CONTAINER_CLEAR_LOCK(sl) boost_container_x86_clear_lock(sl) + + #else /* Win32 MSC */ + #define BOOST_CONTAINER_CAS_LOCK(sl) interlockedexchange((long volatile*)sl, (long)1) + #define BOOST_CONTAINER_CLEAR_LOCK(sl) interlockedexchange((long volatile*)sl, (long)0) + #endif + + /* How to yield for a spin lock */ + #define SPINS_PER_YIELD 63 + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + #define SLEEP_EX_DURATION 50 /* delay for yield/sleep */ + #define SPIN_LOCK_YIELD SleepEx(SLEEP_EX_DURATION, FALSE) + #elif defined (__SVR4) && defined (__sun) /* solaris */ + #include + #define SPIN_LOCK_YIELD thr_yield(); + #elif !defined(LACKS_SCHED_H) + #include + #define SPIN_LOCK_YIELD sched_yield(); + #else + #define SPIN_LOCK_YIELD + #endif /* ... yield ... */ + + #define BOOST_CONTAINER_SPINS_PER_YIELD 63 + inline int boost_interprocess_spin_acquire_lock(int *sl) { + int spins = 0; + while (*(volatile int *)sl != 0 || + BOOST_CONTAINER_CAS_LOCK(sl)) { + if ((++spins & BOOST_CONTAINER_SPINS_PER_YIELD) == 0) { + SPIN_LOCK_YIELD; + } + } + return 0; + } + #define BOOST_CONTAINER_MLOCK_T int + #define BOOST_CONTAINER_TRY_LOCK(sl) !BOOST_CONTAINER_CAS_LOCK(sl) + #define BOOST_CONTAINER_RELEASE_LOCK(sl) BOOST_CONTAINER_CLEAR_LOCK(sl) + #define BOOST_CONTAINER_ACQUIRE_LOCK(sl) (BOOST_CONTAINER_CAS_LOCK(sl)? boost_interprocess_spin_acquire_lock(sl) : 0) + #define BOOST_CONTAINER_INITIAL_LOCK(sl) (*sl = 0) + #define BOOST_CONTAINER_DESTROY_LOCK(sl) (0) +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32 + // +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD + #include +#endif + +namespace boost { +namespace container { +namespace container_detail { + +#if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE + class null_mutex + { + private: + null_mutex(const null_mutex &); + void operator=(const null_mutex &); + + public: + null_mutex() { } + + static void lock() { } + static void unlock() { } + }; + + typedef null_mutex default_mutex; +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS + + class spin_mutex + { + private: + BOOST_CONTAINER_MLOCK_T sl; + spin_mutex(const spin_mutex &); + void operator=(const spin_mutex &); + + public: + spin_mutex() { BOOST_CONTAINER_INITIAL_LOCK(&sl); } + + void lock() { BOOST_CONTAINER_ACQUIRE_LOCK(&sl); } + void unlock() { BOOST_CONTAINER_RELEASE_LOCK(&sl); } + }; + typedef spin_mutex default_mutex; +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32 + class mutex + { + private: + CRITICAL_SECTION mtx; + + mutex(const mutex &); + void operator=(const mutex &); + + public: + mutex() + { InitializeCriticalSection(&mtx); } + + ~mutex() + { DeleteCriticalSection(&mtx); } + + void lock() + { EnterCriticalSection(&mtx); } + + void unlock() + { LeaveCriticalSection(&mtx); } + }; + + typedef mutex default_mutex; +#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD + class mutex + { + private: + pthread_mutex_t mtx; + + mutex(const mutex &); + void operator=(const mutex &); + + public: + mutex() + { pthread_mutex_init(&mtx, 0); } + + ~mutex() + { pthread_mutex_destroy(&mtx); } + + void lock() + { pthread_mutex_lock(&mtx); } + + void unlock() + { pthread_mutex_unlock(&mtx); } + }; + + typedef mutex default_mutex; +#endif + +template +class scoped_lock +{ + public: + scoped_lock(Mutex &m) + : m_(m) + { m_.lock(); } + ~scoped_lock() + { m_.unlock(); } + + private: + Mutex &m_; +}; + +} // namespace container_detail +} // namespace container +} // namespace boost + +#undef BOOST_MUTEX_HELPER_WIN32 +#undef BOOST_MUTEX_HELPER_PTHREAD +#undef BOOST_MUTEX_HELPER_NONE +#undef BOOST_MUTEX_HELPER +#undef BOOST_MUTEX_HELPER_SPINLOCKS + +#include + +#endif diff --git a/boost/container/detail/node_alloc_holder.hpp b/boost/container/detail/node_alloc_holder.hpp new file mode 100644 index 0000000..250c559 --- /dev/null +++ b/boost/container/detail/node_alloc_holder.hpp @@ -0,0 +1,399 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_NODE_ALLOC_HPP_ +#define BOOST_CONTAINER_DETAIL_NODE_ALLOC_HPP_ + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef BOOST_CONTAINER_PERFECT_FORWARDING +#include +#endif + +#include + + + +namespace boost { +namespace container { +namespace container_detail { + +template +struct node_compare + : private ValueCompare +{ + typedef ValueCompare wrapped_value_compare; + typedef typename wrapped_value_compare::key_type key_type; + typedef typename wrapped_value_compare::value_type value_type; + typedef typename wrapped_value_compare::key_of_value key_of_value; + + explicit node_compare(const wrapped_value_compare &pred) + : wrapped_value_compare(pred) + {} + + node_compare() + : wrapped_value_compare() + {} + + wrapped_value_compare &value_comp() + { return static_cast(*this); } + + wrapped_value_compare &value_comp() const + { return static_cast(*this); } + + bool operator()(const Node &a, const Node &b) const + { return wrapped_value_compare::operator()(a.get_data(), b.get_data()); } +}; + +template +struct node_alloc_holder +{ + //If the intrusive container is an associative container, obtain the predicate, which will + //be of type node_compare<>. If not an associative container value_compare will be a "nat" type. + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, ICont, + value_compare, container_detail::nat) intrusive_value_compare; + //In that case obtain the value predicate from the node predicate via wrapped_value_compare + //if intrusive_value_compare is node_compare<>, nat otherwise + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(boost::container::container_detail::, ICont, + wrapped_value_compare, container_detail::nat) value_compare; + + typedef allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::value_type value_type; + typedef ICont intrusive_container; + typedef typename ICont::value_type Node; + typedef typename allocator_traits_type::template + portable_rebind_alloc::type NodeAlloc; + typedef allocator_traits node_allocator_traits_type; + typedef container_detail::allocator_version_traits node_allocator_version_traits_type; + typedef A ValAlloc; + typedef typename node_allocator_traits_type::pointer NodePtr; + typedef container_detail::scoped_deallocator Deallocator; + typedef typename node_allocator_traits_type::size_type size_type; + typedef typename node_allocator_traits_type::difference_type difference_type; + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + typedef container_detail::integral_constant::value> alloc_version; + typedef typename ICont::iterator icont_iterator; + typedef typename ICont::const_iterator icont_citerator; + typedef allocator_destroyer Destroyer; + typedef allocator_traits NodeAllocTraits; + typedef allocator_version_traits AllocVersionTraits; + + private: + BOOST_COPYABLE_AND_MOVABLE(node_alloc_holder) + + public: + + //Constructors for sequence containers + node_alloc_holder() + : members_() + {} + + explicit node_alloc_holder(const ValAlloc &a) + : members_(a) + {} + + explicit node_alloc_holder(const node_alloc_holder &x) + : members_(NodeAllocTraits::select_on_container_copy_construction(x.node_alloc())) + {} + + explicit node_alloc_holder(BOOST_RV_REF(node_alloc_holder) x) + : members_(boost::move(x.node_alloc())) + { this->icont().swap(x.icont()); } + + //Constructors for associative containers + explicit node_alloc_holder(const ValAlloc &a, const value_compare &c) + : members_(a, c) + {} + + explicit node_alloc_holder(const node_alloc_holder &x, const value_compare &c) + : members_(NodeAllocTraits::select_on_container_copy_construction(x.node_alloc()), c) + {} + + explicit node_alloc_holder(const value_compare &c) + : members_(c) + {} + + //helpers for move assignments + explicit node_alloc_holder(BOOST_RV_REF(node_alloc_holder) x, const value_compare &c) + : members_(boost::move(x.node_alloc()), c) + { this->icont().swap(x.icont()); } + + void copy_assign_alloc(const node_alloc_holder &x) + { + container_detail::bool_ flag; + container_detail::assign_alloc( static_cast(this->members_) + , static_cast(x.members_), flag); + } + + void move_assign_alloc( node_alloc_holder &x) + { + container_detail::bool_ flag; + container_detail::move_alloc( static_cast(this->members_) + , static_cast(x.members_), flag); + } + + ~node_alloc_holder() + { this->clear(alloc_version()); } + + size_type max_size() const + { return allocator_traits_type::max_size(this->node_alloc()); } + + NodePtr allocate_one() + { return AllocVersionTraits::allocate_one(this->node_alloc()); } + + void deallocate_one(const NodePtr &p) + { AllocVersionTraits::deallocate_one(this->node_alloc(), p); } + + #ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + template + NodePtr create_node(Args &&...args) + { + NodePtr p = this->allocate_one(); + Deallocator node_deallocator(p, this->node_alloc()); + allocator_traits::construct + ( this->node_alloc() + , container_detail::addressof(p->m_data), boost::forward(args)...); + node_deallocator.release(); + //This does not throw + typedef typename Node::hook_type hook_type; + ::new(static_cast(container_detail::to_raw_pointer(p)), boost_container_new_t()) hook_type; + return (p); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + NodePtr create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + NodePtr p = this->allocate_one(); \ + Deallocator node_deallocator(p, this->node_alloc()); \ + allocator_traits::construct \ + (this->node_alloc(), container_detail::addressof(p->m_data) \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + node_deallocator.release(); \ + typedef typename Node::hook_type hook_type; \ + ::new(static_cast(container_detail::to_raw_pointer(p)), boost_container_new_t()) hook_type; \ + return (p); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + template + NodePtr create_node_from_it(const It &it) + { + NodePtr p = this->allocate_one(); + Deallocator node_deallocator(p, this->node_alloc()); + ::boost::container::construct_in_place(this->node_alloc(), container_detail::addressof(p->m_data), it); + node_deallocator.release(); + //This does not throw + typedef typename Node::hook_type hook_type; + ::new(static_cast(container_detail::to_raw_pointer(p)), boost_container_new_t()) hook_type; + return (p); + } + + void destroy_node(const NodePtr &nodep) + { + allocator_traits::destroy(this->node_alloc(), container_detail::to_raw_pointer(nodep)); + this->deallocate_one(nodep); + } + + void swap(node_alloc_holder &x) + { + this->icont().swap(x.icont()); + container_detail::bool_ flag; + container_detail::swap_alloc(this->node_alloc(), x.node_alloc(), flag); + } + + template + void allocate_many_and_construct + (FwdIterator beg, difference_type n, Inserter inserter) + { + if(n){ + typedef typename node_allocator_version_traits_type::multiallocation_chain multiallocation_chain; + + //Try to allocate memory in a single block + typedef typename multiallocation_chain::iterator multialloc_iterator; + multiallocation_chain mem; + NodeAlloc &nalloc = this->node_alloc(); + node_allocator_version_traits_type::allocate_individual(nalloc, n, mem); + multialloc_iterator itbeg(mem.begin()), itlast(mem.last()); + mem.clear(); + Node *p = 0; + BOOST_TRY{ + Deallocator node_deallocator(NodePtr(), nalloc); + container_detail::scoped_destructor sdestructor(nalloc, 0); + while(n--){ + p = container_detail::to_raw_pointer(iterator_to_pointer(itbeg)); + node_deallocator.set(p); + ++itbeg; + //This can throw + boost::container::construct_in_place(nalloc, container_detail::addressof(p->m_data), beg); + sdestructor.set(p); + ++beg; + //This does not throw + typedef typename Node::hook_type hook_type; + ::new(static_cast(p), boost_container_new_t()) hook_type; + //This can throw in some containers (predicate might throw). + //(sdestructor will destruct the node and node_deallocator will deallocate it in case of exception) + inserter(*p); + sdestructor.set(0); + } + sdestructor.release(); + node_deallocator.release(); + } + BOOST_CATCH(...){ + mem.incorporate_after(mem.last(), &*itbeg, &*itlast, n); + node_allocator_version_traits_type::deallocate_individual(this->node_alloc(), mem); + BOOST_RETHROW + } + BOOST_CATCH_END + } + } + + void clear(allocator_v1) + { this->icont().clear_and_dispose(Destroyer(this->node_alloc())); } + + void clear(allocator_v2) + { + typename NodeAlloc::multiallocation_chain chain; + allocator_destroyer_and_chain_builder builder(this->node_alloc(), chain); + this->icont().clear_and_dispose(builder); + //BOOST_STATIC_ASSERT((::boost::has_move_emulation_enabled::value == true)); + if(!chain.empty()) + this->node_alloc().deallocate_individual(chain); + } + + icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, allocator_v1) + { return this->icont().erase_and_dispose(first, last, Destroyer(this->node_alloc())); } + + icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, allocator_v2) + { + typedef typename NodeAlloc::multiallocation_chain multiallocation_chain; + NodeAlloc & nalloc = this->node_alloc(); + multiallocation_chain chain; + allocator_destroyer_and_chain_builder chain_builder(nalloc, chain); + icont_iterator ret_it = this->icont().erase_and_dispose(first, last, chain_builder); + nalloc.deallocate_individual(chain); + return ret_it; + } + + template + size_type erase_key(const Key& k, const Comparator &comp, allocator_v1) + { return this->icont().erase_and_dispose(k, comp, Destroyer(this->node_alloc())); } + + template + size_type erase_key(const Key& k, const Comparator &comp, allocator_v2) + { + allocator_multialloc_chain_node_deallocator chain_holder(this->node_alloc()); + return this->icont().erase_and_dispose(k, comp, chain_holder.get_chain_builder()); + } + + protected: + struct cloner + { + cloner(node_alloc_holder &holder) + : m_holder(holder) + {} + + NodePtr operator()(const Node &other) const + { return m_holder.create_node(other.get_data()); } + + node_alloc_holder &m_holder; + }; + + struct members_holder + : public NodeAlloc + { + private: + members_holder(const members_holder&); + members_holder & operator=(const members_holder&); + + public: + members_holder() + : NodeAlloc(), m_icont() + {} + + template + explicit members_holder(BOOST_FWD_REF(ConvertibleToAlloc) c2alloc) + : NodeAlloc(boost::forward(c2alloc)) + , m_icont() + {} + + template + members_holder(BOOST_FWD_REF(ConvertibleToAlloc) c2alloc, const value_compare &c) + : NodeAlloc(boost::forward(c2alloc)) + , m_icont(typename ICont::value_compare(c)) + {} + + explicit members_holder(const value_compare &c) + : NodeAlloc() + , m_icont(typename ICont::value_compare(c)) + {} + + //The intrusive container + ICont m_icont; + }; + + ICont &non_const_icont() const + { return const_cast(this->members_.m_icont); } + + ICont &icont() + { return this->members_.m_icont; } + + const ICont &icont() const + { return this->members_.m_icont; } + + NodeAlloc &node_alloc() + { return static_cast(this->members_); } + + const NodeAlloc &node_alloc() const + { return static_cast(this->members_); } + + members_holder members_; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif // BOOST_CONTAINER_DETAIL_NODE_ALLOC_HPP_ diff --git a/boost/container/detail/node_pool.hpp b/boost/container/detail/node_pool.hpp new file mode 100644 index 0000000..60d8266 --- /dev/null +++ b/boost/container/detail/node_pool.hpp @@ -0,0 +1,156 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP +#define BOOST_CONTAINER_DETAIL_NODE_POOL_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include //std::unary_function +#include //std::swap +#include + +namespace boost { +namespace container { +namespace container_detail { + +//!Pooled memory allocator using single segregated storage. Includes +//!a reference count but the class does not delete itself, this is +//!responsibility of user classes. Node size (NodeSize) and the number of +//!nodes allocated per block (NodesPerBlock) are known at compile time +template< std::size_t NodeSize, std::size_t NodesPerBlock > +class private_node_pool + //Inherit from the implementation to avoid template bloat + : public boost::container::container_detail:: + private_node_pool_impl +{ + typedef boost::container::container_detail:: + private_node_pool_impl base_t; + //Non-copyable + private_node_pool(const private_node_pool &); + private_node_pool &operator=(const private_node_pool &); + + public: + typedef typename base_t::multiallocation_chain multiallocation_chain; + static const std::size_t nodes_per_block = NodesPerBlock; + + //!Constructor from a segment manager. Never throws + private_node_pool() + : base_t(0, NodeSize, NodesPerBlock) + {} + +}; + +template< std::size_t NodeSize + , std::size_t NodesPerBlock + > +class shared_node_pool + : public private_node_pool +{ + private: + typedef private_node_pool private_node_allocator_t; + + public: + typedef typename private_node_allocator_t::free_nodes_t free_nodes_t; + typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain; + + //!Constructor from a segment manager. Never throws + shared_node_pool() + : private_node_allocator_t(){} + + //!Destructor. Deallocates all allocated blocks. Never throws + ~shared_node_pool() + {} + + //!Allocates array of count elements. Can throw std::bad_alloc + void *allocate_node() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + return private_node_allocator_t::allocate_node(); + } + + //!Deallocates an array pointed by ptr. Never throws + void deallocate_node(void *ptr) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_node(ptr); + } + + //!Allocates a singly linked list of n nodes ending in null pointer. + //!can throw std::bad_alloc + void allocate_nodes(const std::size_t n, multiallocation_chain &chain) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + return private_node_allocator_t::allocate_nodes(n, chain); + } + + void deallocate_nodes(multiallocation_chain &chain) + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_nodes(chain); + } + + //!Deallocates all the free blocks of memory. Never throws + void deallocate_free_blocks() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::deallocate_free_blocks(); + } + + //!Deallocates all blocks. Never throws + void purge_blocks() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + private_node_allocator_t::purge_blocks(); + } + + std::size_t num_free_nodes() + { + //----------------------- + scoped_lock guard(mutex_); + //----------------------- + return private_node_allocator_t::num_free_nodes(); + } + + private: + default_mutex mutex_; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP diff --git a/boost/container/detail/node_pool_impl.hpp b/boost/container/detail/node_pool_impl.hpp new file mode 100644 index 0000000..2450e51 --- /dev/null +++ b/boost/container/detail/node_pool_impl.hpp @@ -0,0 +1,366 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_IMPL_HPP +#define BOOST_CONTAINER_DETAIL_NODE_POOL_IMPL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +class private_node_pool_impl +{ + //Non-copyable + private_node_pool_impl(); + private_node_pool_impl(const private_node_pool_impl &); + private_node_pool_impl &operator=(const private_node_pool_impl &); + + //A node object will hold node_t when it's not allocated + public: + typedef typename SegmentManagerBase::void_pointer void_pointer; + typedef typename node_slist::slist_hook_t slist_hook_t; + typedef typename node_slist::node_t node_t; + typedef typename node_slist::node_slist_t free_nodes_t; + typedef typename SegmentManagerBase::multiallocation_chain multiallocation_chain; + typedef typename SegmentManagerBase::size_type size_type; + + private: + typedef typename bi::make_slist + < node_t, bi::base_hook + , bi::linear + , bi::constant_time_size >::type blockslist_t; + public: + + //!Segment manager typedef + typedef SegmentManagerBase segment_manager_base_type; + + //!Constructor from a segment manager. Never throws + private_node_pool_impl(segment_manager_base_type *segment_mngr_base, size_type node_size, size_type nodes_per_block) + : m_nodes_per_block(nodes_per_block) + , m_real_node_size(lcm(node_size, size_type(alignment_of::value))) + //General purpose allocator + , mp_segment_mngr_base(segment_mngr_base) + , m_blocklist() + , m_freelist() + //Debug node count + , m_allocated(0) + {} + + //!Destructor. Deallocates all allocated blocks. Never throws + ~private_node_pool_impl() + { this->purge_blocks(); } + + size_type get_real_num_node() const + { return m_nodes_per_block; } + + //!Returns the segment manager. Never throws + segment_manager_base_type* get_segment_manager_base()const + { return container_detail::to_raw_pointer(mp_segment_mngr_base); } + + void *allocate_node() + { return this->priv_alloc_node(); } + + //!Deallocates an array pointed by ptr. Never throws + void deallocate_node(void *ptr) + { this->priv_dealloc_node(ptr); } + + //!Allocates a singly linked list of n nodes ending in null pointer. + void allocate_nodes(const size_type n, multiallocation_chain &chain) + { + //Preallocate all needed blocks to fulfill the request + size_type cur_nodes = m_freelist.size(); + if(cur_nodes < n){ + this->priv_alloc_block(((n - cur_nodes) - 1)/m_nodes_per_block + 1); + } + + //We just iterate the needed nodes to get the last we'll erase + typedef typename free_nodes_t::iterator free_iterator; + free_iterator before_last_new_it = m_freelist.before_begin(); + for(size_type j = 0; j != n; ++j){ + ++before_last_new_it; + } + + //Cache the first node of the allocated range before erasing + free_iterator first_node(m_freelist.begin()); + free_iterator last_node (before_last_new_it); + + //Erase the range. Since we already have the distance, this is O(1) + m_freelist.erase_after( m_freelist.before_begin() + , ++free_iterator(before_last_new_it) + , n); + + //Now take the last erased node and just splice it in the end + //of the intrusive list that will be traversed by the multialloc iterator. + chain.incorporate_after(chain.before_begin(), &*first_node, &*last_node, n); + m_allocated += n; + } + + void deallocate_nodes(multiallocation_chain &chain) + { + typedef typename multiallocation_chain::iterator iterator; + iterator it(chain.begin()), itend(chain.end()); + while(it != itend){ + void *pElem = &*it; + ++it; + this->priv_dealloc_node(pElem); + } + } + + //!Deallocates all the free blocks of memory. Never throws + void deallocate_free_blocks() + { + typedef typename free_nodes_t::iterator nodelist_iterator; + typename blockslist_t::iterator bit(m_blocklist.before_begin()), + it(m_blocklist.begin()), + itend(m_blocklist.end()); + free_nodes_t backup_list; + nodelist_iterator backup_list_last = backup_list.before_begin(); + + //Execute the algorithm and get an iterator to the last value + size_type blocksize = get_rounded_size + (m_real_node_size*m_nodes_per_block, (size_type) alignment_of::value); + + while(it != itend){ + //Collect all the nodes from the block pointed by it + //and push them in the list + free_nodes_t free_nodes; + nodelist_iterator last_it = free_nodes.before_begin(); + const void *addr = get_block_from_hook(&*it, blocksize); + + m_freelist.remove_and_dispose_if + (is_between(addr, blocksize), push_in_list(free_nodes, last_it)); + + //If the number of nodes is equal to m_nodes_per_block + //this means that the block can be deallocated + if(free_nodes.size() == m_nodes_per_block){ + //Unlink the nodes + free_nodes.clear(); + it = m_blocklist.erase_after(bit); + mp_segment_mngr_base->deallocate((void*)addr); + } + //Otherwise, insert them in the backup list, since the + //next "remove_if" does not need to check them again. + else{ + //Assign the iterator to the last value if necessary + if(backup_list.empty() && !m_freelist.empty()){ + backup_list_last = last_it; + } + //Transfer nodes. This is constant time. + backup_list.splice_after + ( backup_list.before_begin() + , free_nodes + , free_nodes.before_begin() + , last_it + , free_nodes.size()); + bit = it; + ++it; + } + } + //We should have removed all the nodes from the free list + BOOST_ASSERT(m_freelist.empty()); + + //Now pass all the node to the free list again + m_freelist.splice_after + ( m_freelist.before_begin() + , backup_list + , backup_list.before_begin() + , backup_list_last + , backup_list.size()); + } + + size_type num_free_nodes() + { return m_freelist.size(); } + + //!Deallocates all used memory. Precondition: all nodes allocated from this pool should + //!already be deallocated. Otherwise, undefined behaviour. Never throws + void purge_blocks() + { + //check for memory leaks + BOOST_ASSERT(m_allocated==0); + size_type blocksize = get_rounded_size + (m_real_node_size*m_nodes_per_block, (size_type)alignment_of::value); + + //We iterate though the NodeBlock list to free the memory + while(!m_blocklist.empty()){ + void *addr = get_block_from_hook(&m_blocklist.front(), blocksize); + m_blocklist.pop_front(); + mp_segment_mngr_base->deallocate((void*)addr); + } + //Just clear free node list + m_freelist.clear(); + } + + void swap(private_node_pool_impl &other) + { + BOOST_ASSERT(m_nodes_per_block == other.m_nodes_per_block); + BOOST_ASSERT(m_real_node_size == other.m_real_node_size); + std::swap(mp_segment_mngr_base, other.mp_segment_mngr_base); + m_blocklist.swap(other.m_blocklist); + m_freelist.swap(other.m_freelist); + std::swap(m_allocated, other.m_allocated); + } + + private: + + struct push_in_list + { + push_in_list(free_nodes_t &l, typename free_nodes_t::iterator &it) + : slist_(l), last_it_(it) + {} + + void operator()(typename free_nodes_t::pointer p) const + { + slist_.push_front(*p); + if(slist_.size() == 1){ //Cache last element + ++last_it_ = slist_.begin(); + } + } + + private: + free_nodes_t &slist_; + typename free_nodes_t::iterator &last_it_; + }; + + struct is_between + { + typedef typename free_nodes_t::value_type argument_type; + typedef bool result_type; + + is_between(const void *addr, std::size_t size) + : beg_(static_cast(addr)), end_(beg_+size) + {} + + bool operator()(typename free_nodes_t::const_reference v) const + { + return (beg_ <= reinterpret_cast(&v) && + end_ > reinterpret_cast(&v)); + } + private: + const char * beg_; + const char * end_; + }; + + //!Allocates one node, using single segregated storage algorithm. + //!Never throws + node_t *priv_alloc_node() + { + //If there are no free nodes we allocate a new block + if (m_freelist.empty()) + this->priv_alloc_block(1); + //We take the first free node + node_t *n = (node_t*)&m_freelist.front(); + m_freelist.pop_front(); + ++m_allocated; + return n; + } + + //!Deallocates one node, using single segregated storage algorithm. + //!Never throws + void priv_dealloc_node(void *pElem) + { + //We put the node at the beginning of the free node list + node_t * to_deallocate = static_cast(pElem); + m_freelist.push_front(*to_deallocate); + BOOST_ASSERT(m_allocated>0); + --m_allocated; + } + + //!Allocates several blocks of nodes. Can throw + void priv_alloc_block(size_type num_blocks) + { + BOOST_ASSERT(num_blocks > 0); + size_type blocksize = + get_rounded_size(m_real_node_size*m_nodes_per_block, (size_type)alignment_of::value); + + BOOST_TRY{ + for(size_type i = 0; i != num_blocks; ++i){ + //We allocate a new NodeBlock and put it as first + //element in the free Node list + char *pNode = reinterpret_cast + (mp_segment_mngr_base->allocate(blocksize + sizeof(node_t))); + char *pBlock = pNode; + m_blocklist.push_front(get_block_hook(pBlock, blocksize)); + + //We initialize all Nodes in Node Block to insert + //them in the free Node list + for(size_type j = 0; j < m_nodes_per_block; ++j, pNode += m_real_node_size){ + m_freelist.push_front(*new (pNode) node_t); + } + } + } + BOOST_CATCH(...){ + //to-do: if possible, an efficient way to deallocate allocated blocks + BOOST_RETHROW + } + BOOST_CATCH_END + } + + //!Deprecated, use deallocate_free_blocks + void deallocate_free_chunks() + { this->deallocate_free_blocks(); } + + //!Deprecated, use purge_blocks + void purge_chunks() + { this->purge_blocks(); } + + private: + //!Returns a reference to the block hook placed in the end of the block + static node_t & get_block_hook (void *block, size_type blocksize) + { + return *reinterpret_cast(reinterpret_cast(block) + blocksize); + } + + //!Returns the starting address of the block reference to the block hook placed in the end of the block + void *get_block_from_hook (node_t *hook, size_type blocksize) + { + return (reinterpret_cast(hook) - blocksize); + } + + private: + typedef typename boost::intrusive::pointer_traits + ::template rebind_pointer::type segment_mngr_base_ptr_t; + + const size_type m_nodes_per_block; + const size_type m_real_node_size; + segment_mngr_base_ptr_t mp_segment_mngr_base; //Segment manager + blockslist_t m_blocklist; //Intrusive container of blocks + free_nodes_t m_freelist; //Intrusive container of free nods + size_type m_allocated; //Used nodes for debugging +}; + + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP diff --git a/boost/container/detail/pair.hpp b/boost/container/detail/pair.hpp new file mode 100644 index 0000000..b7ad84c --- /dev/null +++ b/boost/container/detail/pair.hpp @@ -0,0 +1,374 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_PAIR_HPP +#define BOOST_CONTAINER_CONTAINER_DETAIL_PAIR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include + +#include //std::pair +#include //std::swap + +#include + + +#ifndef BOOST_CONTAINER_PERFECT_FORWARDING +#include +#endif + +namespace boost { +namespace container { +namespace container_detail { + +template +struct pair; + +template +struct is_pair +{ + static const bool value = false; +}; + +template +struct is_pair< pair > +{ + static const bool value = true; +}; + +template +struct is_pair< std::pair > +{ + static const bool value = true; +}; + +struct pair_nat; + +struct piecewise_construct_t { }; +static const piecewise_construct_t piecewise_construct = piecewise_construct_t(); + +/* +template +struct pair +{ + template pair(pair&& p); + template + pair(piecewise_construct_t, tuple first_args, + tuple second_args); + + template pair& operator=(const pair& p); + pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value); + template pair& operator=(pair&& p); + + void swap(pair& p) noexcept(noexcept(swap(first, p.first)) && + noexcept(swap(second, p.second))); +}; + +template bool operator==(const pair&, const pair&); +template bool operator!=(const pair&, const pair&); +template bool operator< (const pair&, const pair&); +template bool operator> (const pair&, const pair&); +template bool operator>=(const pair&, const pair&); +template bool operator<=(const pair&, const pair&); +*/ + + +template +struct pair +{ + private: + BOOST_COPYABLE_AND_MOVABLE(pair) + + public: + typedef T1 first_type; + typedef T2 second_type; + + T1 first; + T2 second; + + //Default constructor + pair() + : first(), second() + {} + + //pair copy assignment + pair(const pair& x) + : first(x.first), second(x.second) + {} + + //pair move constructor + pair(BOOST_RV_REF(pair) p) + : first(::boost::move(p.first)), second(::boost::move(p.second)) + {} + + template + pair(const pair &p) + : first(p.first), second(p.second) + {} + + template + pair(BOOST_RV_REF_BEG pair BOOST_RV_REF_END p) + : first(::boost::move(p.first)), second(::boost::move(p.second)) + {} + + //pair from two values + pair(const T1 &t1, const T2 &t2) + : first(t1) + , second(t2) + {} + + template + pair(BOOST_FWD_REF(U) u, BOOST_FWD_REF(V) v) + : first(::boost::forward(u)) + , second(::boost::forward(v)) + {} + + //And now compatibility with std::pair + pair(const std::pair& x) + : first(x.first), second(x.second) + {} + + template + pair(const std::pair& p) + : first(p.first), second(p.second) + {} + + pair(BOOST_RV_REF_BEG std::pair BOOST_RV_REF_END p) + : first(::boost::move(p.first)), second(::boost::move(p.second)) + {} + + template + pair(BOOST_RV_REF_BEG std::pair BOOST_RV_REF_END p) + : first(::boost::move(p.first)), second(::boost::move(p.second)) + {} + + //piecewise_construct missing + //template pair(pair&& p); + //template + // pair(piecewise_construct_t, tuple first_args, + // tuple second_args); +/* + //Variadic versions + template + pair(BOOST_CONTAINER_PP_PARAM(U, u), typename container_detail::disable_if + < container_detail::is_pair< typename container_detail::remove_ref_const::type >, pair_nat>::type* = 0) + : first(::boost::forward(u)) + , second() + {} + + #ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + template + pair(U &&u, V &&v) + : first(::boost::forward(u)) + , second(::boost::forward(v), ::boost::forward(args)...) + {} + + #else + + #define BOOST_PP_LOCAL_MACRO(n) \ + template \ + pair(BOOST_CONTAINER_PP_PARAM(U, u) \ + ,BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + : first(::boost::forward(u)) \ + , second(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)) \ + {} \ + //! + #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + #endif +*/ + //pair copy assignment + pair& operator=(BOOST_COPY_ASSIGN_REF(pair) p) + { + first = p.first; + second = p.second; + return *this; + } + + //pair move assignment + pair& operator=(BOOST_RV_REF(pair) p) + { + first = ::boost::move(p.first); + second = ::boost::move(p.second); + return *this; + } + + template + typename ::boost::container::container_detail::enable_if_c + < !(::boost::container::container_detail::is_same::value && + ::boost::container::container_detail::is_same::value) + , pair &>::type + operator=(const pair&p) + { + first = p.first; + second = p.second; + return *this; + } + + template + typename ::boost::container::container_detail::enable_if_c + < !(::boost::container::container_detail::is_same::value && + ::boost::container::container_detail::is_same::value) + , pair &>::type + operator=(BOOST_RV_REF_BEG pair BOOST_RV_REF_END p) + { + first = ::boost::move(p.first); + second = ::boost::move(p.second); + return *this; + } + + //std::pair copy assignment + pair& operator=(const std::pair &p) + { + first = p.first; + second = p.second; + return *this; + } + + template + pair& operator=(const std::pair &p) + { + first = ::boost::move(p.first); + second = ::boost::move(p.second); + return *this; + } + + //std::pair move assignment + pair& operator=(BOOST_RV_REF_BEG std::pair BOOST_RV_REF_END p) + { + first = ::boost::move(p.first); + second = ::boost::move(p.second); + return *this; + } + + template + pair& operator=(BOOST_RV_REF_BEG std::pair BOOST_RV_REF_END p) + { + first = ::boost::move(p.first); + second = ::boost::move(p.second); + return *this; + } + + //swap + void swap(pair& p) + { + using std::swap; + swap(this->first, p.first); + swap(this->second, p.second); + } +}; + +template +inline bool operator==(const pair& x, const pair& y) +{ return static_cast(x.first == y.first && x.second == y.second); } + +template +inline bool operator< (const pair& x, const pair& y) +{ return static_cast(x.first < y.first || + (!(y.first < x.first) && x.second < y.second)); } + +template +inline bool operator!=(const pair& x, const pair& y) +{ return static_cast(!(x == y)); } + +template +inline bool operator> (const pair& x, const pair& y) +{ return y < x; } + +template +inline bool operator>=(const pair& x, const pair& y) +{ return static_cast(!(x < y)); } + +template +inline bool operator<=(const pair& x, const pair& y) +{ return static_cast(!(y < x)); } + +template +inline pair make_pair(T1 x, T2 y) +{ return pair(x, y); } + +template +inline void swap(pair& x, pair& y) +{ + swap(x.first, y.first); + swap(x.second, y.second); +} + +} //namespace container_detail { +} //namespace container { + + +//Without this specialization recursive flat_(multi)map instantiation fails +//because is_enum needs to instantiate the recursive pair, leading to a compilation error). +//This breaks the cycle clearly stating that pair is not an enum avoiding any instantiation. +template +struct is_enum; + +template +struct is_enum< ::boost::container::container_detail::pair > +{ + static const bool value = false; +}; + +template +struct is_class; + +//This specialization is needed to avoid instantiation of pair in +//is_class, and allow recursive maps. +template +struct is_class< ::boost::container::container_detail::pair > +{ + static const bool value = true; +}; + +#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES + +template +struct has_move_emulation_enabled< ::boost::container::container_detail::pair > +{ + static const bool value = true; +}; + +#endif + +namespace move_detail{ + +template +struct is_class_or_union; + +template +struct is_class_or_union< ::boost::container::container_detail::pair > +//This specialization is needed to avoid instantiation of pair in +//is_class, and allow recursive maps. +{ + static const bool value = true; +}; + + +} //namespace move_detail{ + +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_PAIR_HPP diff --git a/boost/container/detail/placement_new.hpp b/boost/container/detail/placement_new.hpp new file mode 100644 index 0000000..2489d8a --- /dev/null +++ b/boost/container/detail/placement_new.hpp @@ -0,0 +1,27 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP +#define BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +struct boost_container_new_t{}; + +//avoid including +inline void *operator new(std::size_t, void *p, boost_container_new_t) +{ return p; } + +inline void operator delete(void *, void *, boost_container_new_t) +{} + +#endif //BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP diff --git a/boost/container/detail/pool_common.hpp b/boost/container/detail/pool_common.hpp new file mode 100644 index 0000000..11fb6c2 --- /dev/null +++ b/boost/container/detail/pool_common.hpp @@ -0,0 +1,53 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_POOL_COMMON_HPP +#define BOOST_CONTAINER_DETAIL_POOL_COMMON_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +struct node_slist +{ + //This hook will be used to chain the individual nodes + typedef typename bi::make_slist_base_hook + , bi::link_mode >::type slist_hook_t; + + //A node object will hold node_t when it's not allocated + typedef slist_hook_t node_t; + + typedef typename bi::make_slist + , bi::cache_last, bi::base_hook >::type node_slist_t; +}; + +template +struct is_stateless_segment_manager +{ + static const bool value = false; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP diff --git a/boost/container/detail/pool_common_alloc.hpp b/boost/container/detail/pool_common_alloc.hpp new file mode 100644 index 0000000..dfae7ef --- /dev/null +++ b/boost/container/detail/pool_common_alloc.hpp @@ -0,0 +1,98 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_POOL_COMMON_ALLOC_HPP +#define BOOST_CONTAINER_DETAIL_POOL_COMMON_ALLOC_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +#include +#include +#include +#include + +namespace boost{ +namespace container{ +namespace container_detail{ + +struct node_slist_helper + : public boost::container::container_detail::node_slist +{}; + +struct fake_segment_manager +{ + typedef void * void_pointer; + static const std::size_t PayloadPerAllocation = BOOST_CONTAINER_ALLOCATION_PAYLOAD; + + typedef boost::container::container_detail:: + basic_multiallocation_chain multiallocation_chain; + static void deallocate(void_pointer p) + { boost_cont_free(p); } + + static void deallocate_many(multiallocation_chain &chain) + { + std::size_t size = chain.size(); + std::pair ptrs = chain.extract_data(); + boost_cont_memchain dlchain; + BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&dlchain, ptrs.first, ptrs.second, size); + boost_cont_multidealloc(&dlchain); + } + + typedef std::ptrdiff_t difference_type; + typedef std::size_t size_type; + + static void *allocate_aligned(std::size_t nbytes, std::size_t alignment) + { + void *ret = boost_cont_memalign(nbytes, alignment); + if(!ret) + boost::container::throw_bad_alloc(); + return ret; + } + + static void *allocate(std::size_t nbytes) + { + void *ret = boost_cont_malloc(nbytes); + if(!ret) + boost::container::throw_bad_alloc(); + return ret; + } +}; + +} //namespace boost{ +} //namespace container{ +} //namespace container_detail{ + +namespace boost { +namespace container { +namespace container_detail { + +template +struct is_stateless_segment_manager; + +template<> +struct is_stateless_segment_manager + +{ + static const bool value = true; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //BOOST_CONTAINER_DETAIL_POOL_COMMON_ALLOC_HPP diff --git a/boost/container/detail/preprocessor.hpp b/boost/container/detail/preprocessor.hpp new file mode 100644 index 0000000..838eff2 --- /dev/null +++ b/boost/container/detail/preprocessor.hpp @@ -0,0 +1,228 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_PREPROCESSOR_HPP +#define BOOST_CONTAINER_DETAIL_PREPROCESSOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +#ifdef BOOST_CONTAINER_PERFECT_FORWARDING +//#error "This file is not needed when perfect forwarding is available" +#endif //BOOST_CONTAINER_PERFECT_FORWARDING + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS 10 + +//Note: +//We define template parameters as const references to +//be able to bind temporaries. After that we will un-const them. +//This cast is ugly but it is necessary until "perfect forwarding" +//is achieved in C++0x. Meanwhile, if we want to be able to +//bind rvalues with non-const references, we have to be ugly +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + #define BOOST_CONTAINER_PP_PARAM_LIST(z, n, data) \ + BOOST_PP_CAT(P, n) && BOOST_PP_CAT(p, n) \ + //! +#else + #define BOOST_CONTAINER_PP_PARAM_LIST(z, n, data) \ + const BOOST_PP_CAT(P, n) & BOOST_PP_CAT(p, n) \ + //! +#endif //#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +#define BOOST_CONTAINER_PP_CONST_REF_PARAM_LIST_Q(z, n, Data) \ +const BOOST_PP_CAT(Q, n) & BOOST_PP_CAT(q, n) \ +//! + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + #define BOOST_CONTAINER_PP_PARAM(U, u) \ + U && u \ + //! +#else + #define BOOST_CONTAINER_PP_PARAM(U, u) \ + const U & u \ + //! +#endif //#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + + #define BOOST_CONTAINER_PP_PARAM_INIT(z, n, data) \ + BOOST_PP_CAT(m_p, n) (::boost::forward< BOOST_PP_CAT(P, n) >( BOOST_PP_CAT(p, n) )) \ + //! + +#else //BOOST_NO_CXX11_RVALUE_REFERENCES + + #define BOOST_CONTAINER_PP_PARAM_INIT(z, n, data) \ + BOOST_PP_CAT(m_p, n) (const_cast(BOOST_PP_CAT(p, n))) \ + //! +#endif //#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + + #if defined(BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG) + + namespace boost { + namespace container { + namespace container_detail { + template + struct ref_holder; + + template + struct ref_holder + { + explicit ref_holder(T &t) + : t_(t) + {} + T &t_; + T & get() { return t_; } + }; + + template + struct ref_holder + { + explicit ref_holder(const T &t) + : t_(t) + {} + const T &t_; + const T & get() { return t_; } + }; + + template + struct ref_holder + { + explicit ref_holder(const T &t) + : t_(t) + {} + const T &t_; + const T & get() { return t_; } + }; + + template + struct ref_holder + { + explicit ref_holder(T &&t) + : t_(t) + {} + T &t_; + T && get() { return ::boost::move(t_); } + }; + + template + struct ref_holder + { + explicit ref_holder(T &&t) + : t_(t) + {} + T &t_; + T && get() { return ::boost::move(t_); } + }; + + } //namespace container_detail { + } //namespace container { + } //namespace boost { + + #define BOOST_CONTAINER_PP_PARAM_DEFINE(z, n, data) \ + ::boost::container::container_detail::ref_holder BOOST_PP_CAT(m_p, n); \ + //! + + #else //BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG + + #define BOOST_CONTAINER_PP_PARAM_DEFINE(z, n, data) \ + BOOST_PP_CAT(P, n) && BOOST_PP_CAT(m_p, n); \ + //! + + #endif //defined(BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG) + +#else //BOOST_NO_CXX11_RVALUE_REFERENCES + + #define BOOST_CONTAINER_PP_PARAM_DEFINE(z, n, data) \ + BOOST_PP_CAT(P, n) & BOOST_PP_CAT(m_p, n); \ + //! +#endif //#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && defined(BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG) + + #define BOOST_CONTAINER_PP_MEMBER_FORWARD(z, n, data) BOOST_PP_CAT(this->m_p, n).get() \ + //! + +#else //!defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && defined(BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG) + + #define BOOST_CONTAINER_PP_MEMBER_FORWARD(z, n, data) \ + ::boost::forward< BOOST_PP_CAT(P, n) >( BOOST_PP_CAT(this->m_p, n) ) \ + //! + +#endif //!defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && defined(BOOST_MOVE_MSVC_10_MEMBER_RVALUE_REF_BUG) + +#define BOOST_CONTAINER_PP_PARAM_INC(z, n, data) \ + BOOST_PP_CAT(++this->m_p, n) \ +//! + +#define BOOST_CONTAINER_PP_IDENTITY(z, n, data) data + + +#define BOOST_CONTAINER_PP_PARAM_FORWARD(z, n, data) \ +::boost::forward< BOOST_PP_CAT(P, n) >( BOOST_PP_CAT(p, n) ) \ +//! + +#define BOOST_CONTAINER_PP_DECLVAL(z, n, data) \ +::boost::move_detail::declval< BOOST_PP_CAT(P, n) >() \ +//! + +#define BOOST_CONTAINER_PP_MEMBER_IT_FORWARD(z, n, data) \ +BOOST_PP_CAT(*this->m_p, n) \ +//! + +#define BOOST_CONTAINER_PP_TEMPLATE_PARAM_VOID_DEFAULT(z, n, data) \ + BOOST_PP_CAT(class P, n) = void \ +//! + +#define BOOST_CONTAINER_PP_TEMPLATE_PARAM_WITH_DEFAULT(z, n, default_type) \ + BOOST_PP_CAT(class P, n) = default_type \ +//! + +#define BOOST_CONTAINER_PP_STATIC_PARAM_REF_DECLARE(z, n, data) \ + static BOOST_PP_CAT(P, n) & BOOST_PP_CAT(p, n); \ +//! + +#define BOOST_CONTAINER_PP_PARAM_PASS(z, n, data) \ + BOOST_PP_CAT(p, n) \ +//! + +#define BOOST_CONTAINER_PP_FWD_TYPE(z, n, data) \ + typename ::boost::move_detail::forward_type< BOOST_PP_CAT(P, n) >::type \ +//! + +#include + +//#else + +//#ifdef BOOST_CONTAINER_PERFECT_FORWARDING +//#error "This file is not needed when perfect forwarding is available" +//#endif //BOOST_CONTAINER_PERFECT_FORWARDING + +#endif //#ifndef BOOST_CONTAINER_DETAIL_PREPROCESSOR_HPP diff --git a/boost/container/detail/singleton.hpp b/boost/container/detail/singleton.hpp new file mode 100644 index 0000000..a2372c3 --- /dev/null +++ b/boost/container/detail/singleton.hpp @@ -0,0 +1,117 @@ +// Copyright (C) 2000 Stephen Cleary +// Copyright (C) 2008 Ion Gaztanaga +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org for updates, documentation, and revision history. +// +// This file is a modified file from Boost.Pool + +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP +#define BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +// +// The following helper classes are placeholders for a generic "singleton" +// class. The classes below support usage of singletons, including use in +// program startup/shutdown code, AS LONG AS there is only one thread +// running before main() begins, and only one thread running after main() +// exits. +// +// This class is also limited in that it can only provide singleton usage for +// classes with default constructors. +// + +// The design of this class is somewhat twisted, but can be followed by the +// calling inheritance. Let us assume that there is some user code that +// calls "singleton_default::instance()". The following (convoluted) +// sequence ensures that the same function will be called before main(): +// instance() contains a call to create_object.do_nothing() +// Thus, object_creator is implicitly instantiated, and create_object +// must exist. +// Since create_object is a static member, its constructor must be +// called before main(). +// The constructor contains a call to instance(), thus ensuring that +// instance() will be called before main(). +// The first time instance() is called (i.e., before main()) is the +// latest point in program execution where the object of type T +// can be created. +// Thus, any call to instance() will auto-magically result in a call to +// instance() before main(), unless already present. +// Furthermore, since the instance() function contains the object, instead +// of the singleton_default class containing a static instance of the +// object, that object is guaranteed to be constructed (at the latest) in +// the first call to instance(). This permits calls to instance() from +// static code, even if that code is called before the file-scope objects +// in this file have been initialized. + +namespace boost { +namespace container { +namespace container_detail { + +// T must be: no-throw default constructible and no-throw destructible +template +struct singleton_default +{ + private: + struct object_creator + { + // This constructor does nothing more than ensure that instance() + // is called before main() begins, thus creating the static + // T object before multithreading race issues can come up. + object_creator() { singleton_default::instance(); } + inline void do_nothing() const { } + }; + static object_creator create_object; + + singleton_default(); + + public: + typedef T object_type; + + // If, at any point (in user code), singleton_default::instance() + // is called, then the following function is instantiated. + static object_type & instance() + { + // This is the object that we return a reference to. + // It is guaranteed to be created before main() begins because of + // the next line. + static object_type obj; + + // The following line does nothing else than force the instantiation + // of singleton_default::create_object, whose constructor is + // called before main() begins. + create_object.do_nothing(); + + return obj; + } +}; +template +typename singleton_default::object_creator +singleton_default::create_object; + +} // namespace container_detail +} // namespace container +} // namespace boost + +#include + +#endif //BOOST_CONTAINER_DETAIL_SINGLETON_DETAIL_HPP diff --git a/boost/container/detail/std_fwd.hpp b/boost/container/detail/std_fwd.hpp new file mode 100644 index 0000000..a2edecc --- /dev/null +++ b/boost/container/detail/std_fwd.hpp @@ -0,0 +1,59 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_STD_FWD_HPP +#define BOOST_CONTAINER_DETAIL_STD_FWD_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +////////////////////////////////////////////////////////////////////////////// +// Standard predeclarations +////////////////////////////////////////////////////////////////////////////// + +#if defined(__clang__) && defined(_LIBCPP_VERSION) + #define BOOST_CONTAINER_CLANG_INLINE_STD_NS + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wc++11-extensions" + #define BOOST_CONTAINER_STD_NS_BEG _LIBCPP_BEGIN_NAMESPACE_STD + #define BOOST_CONTAINER_STD_NS_END _LIBCPP_END_NAMESPACE_STD +#else + #define BOOST_CONTAINER_STD_NS_BEG namespace std{ + #define BOOST_CONTAINER_STD_NS_END } +#endif + +BOOST_CONTAINER_STD_NS_BEG + +template +class allocator; + +template +struct less; + +template +struct pair; + +template +struct char_traits; + +struct input_iterator_tag; +struct forward_iterator_tag; +struct bidirectional_iterator_tag; +struct random_access_iterator_tag; + +BOOST_CONTAINER_STD_NS_END + +#ifdef BOOST_CONTAINER_CLANG_INLINE_STD_NS + #pragma GCC diagnostic pop + #undef BOOST_CONTAINER_CLANG_INLINE_STD_NS +#endif //BOOST_CONTAINER_CLANG_INLINE_STD_NS + +#endif //#ifndef BOOST_CONTAINER_DETAIL_STD_FWD_HPP diff --git a/boost/container/detail/transform_iterator.hpp b/boost/container/detail/transform_iterator.hpp new file mode 100644 index 0000000..c4e746c --- /dev/null +++ b/boost/container/detail/transform_iterator.hpp @@ -0,0 +1,177 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// (C) Copyright Gennaro Prota 2003 - 2004. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP +#define BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include + +namespace boost { +namespace container { + +template +struct operator_arrow_proxy +{ + operator_arrow_proxy(const PseudoReference &px) + : m_value(px) + {} + + typedef PseudoReference element_type; + + PseudoReference* operator->() const { return &m_value; } + + mutable PseudoReference m_value; +}; + +template +struct operator_arrow_proxy +{ + operator_arrow_proxy(T &px) + : m_value(px) + {} + + typedef T element_type; + + T* operator->() const { return const_cast(&m_value); } + + T &m_value; +}; + +template +class transform_iterator + : public UnaryFunction + , public std::iterator + < typename Iterator::iterator_category + , typename container_detail::remove_reference::type + , typename Iterator::difference_type + , operator_arrow_proxy + , typename UnaryFunction::result_type> +{ + public: + explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction()) + : UnaryFunction(f), m_it(it) + {} + + explicit transform_iterator() + : UnaryFunction(), m_it() + {} + + //Constructors + transform_iterator& operator++() + { increment(); return *this; } + + transform_iterator operator++(int) + { + transform_iterator result (*this); + increment(); + return result; + } + + friend bool operator== (const transform_iterator& i, const transform_iterator& i2) + { return i.equal(i2); } + + friend bool operator!= (const transform_iterator& i, const transform_iterator& i2) + { return !(i == i2); } + +/* + friend bool operator> (const transform_iterator& i, const transform_iterator& i2) + { return i2 < i; } + + friend bool operator<= (const transform_iterator& i, const transform_iterator& i2) + { return !(i > i2); } + + friend bool operator>= (const transform_iterator& i, const transform_iterator& i2) + { return !(i < i2); } +*/ + friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2) + { return i2.distance_to(i); } + + //Arithmetic + transform_iterator& operator+=(typename Iterator::difference_type off) + { this->advance(off); return *this; } + + transform_iterator operator+(typename Iterator::difference_type off) const + { + transform_iterator other(*this); + other.advance(off); + return other; + } + + friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right) + { return right + off; } + + transform_iterator& operator-=(typename Iterator::difference_type off) + { this->advance(-off); return *this; } + + transform_iterator operator-(typename Iterator::difference_type off) const + { return *this + (-off); } + + typename UnaryFunction::result_type operator*() const + { return dereference(); } + + operator_arrow_proxy + operator->() const + { return operator_arrow_proxy(dereference()); } + + Iterator & base() + { return m_it; } + + const Iterator & base() const + { return m_it; } + + private: + Iterator m_it; + + void increment() + { ++m_it; } + + void decrement() + { --m_it; } + + bool equal(const transform_iterator &other) const + { return m_it == other.m_it; } + + bool less(const transform_iterator &other) const + { return other.m_it < m_it; } + + typename UnaryFunction::result_type dereference() const + { return UnaryFunction::operator()(*m_it); } + + void advance(typename Iterator::difference_type n) + { std::advance(m_it, n); } + + typename Iterator::difference_type distance_to(const transform_iterator &other)const + { return std::distance(other.m_it, m_it); } +}; + +template +transform_iterator +make_transform_iterator(Iterator it, UnaryFunc fun) +{ + return transform_iterator(it, fun); +} + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP diff --git a/boost/container/detail/tree.hpp b/boost/container/detail/tree.hpp new file mode 100644 index 0000000..e59bca0 --- /dev/null +++ b/boost/container/detail/tree.hpp @@ -0,0 +1,1182 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_TREE_HPP +#define BOOST_CONTAINER_TREE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +#include +#include +#include +#include +#include +// +#include +#include +#include +// +#ifndef BOOST_CONTAINER_PERFECT_FORWARDING +#include +#endif + +#include //std::pair +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +struct tree_value_compare + : public KeyCompare +{ + typedef Value value_type; + typedef KeyCompare key_compare; + typedef KeyOfValue key_of_value; + typedef Key key_type; + + explicit tree_value_compare(const key_compare &kcomp) + : KeyCompare(kcomp) + {} + + tree_value_compare() + : KeyCompare() + {} + + const key_compare &key_comp() const + { return static_cast(*this); } + + key_compare &key_comp() + { return static_cast(*this); } + + template + struct is_key + { + static const bool value = is_same::value; + }; + + template + typename enable_if_c::value, const key_type &>::type + key_forward(const T &key) const + { return key; } + + template + typename enable_if_c::value, const key_type &>::type + key_forward(const T &key) const + { return KeyOfValue()(key); } + + template + bool operator()(const KeyType &key1, const KeyType2 &key2) const + { return key_compare::operator()(this->key_forward(key1), this->key_forward(key2)); } +}; + +template +struct intrusive_tree_hook; + +template +struct intrusive_tree_hook +{ + typedef typename container_detail::bi::make_set_base_hook + < container_detail::bi::void_pointer + , container_detail::bi::link_mode + , container_detail::bi::optimize_size + >::type type; +}; + +template +struct intrusive_tree_hook +{ + typedef typename container_detail::bi::make_avl_set_base_hook + < container_detail::bi::void_pointer + , container_detail::bi::link_mode + , container_detail::bi::optimize_size + >::type type; +}; + +template +struct intrusive_tree_hook +{ + typedef typename container_detail::bi::make_bs_set_base_hook + < container_detail::bi::void_pointer + , container_detail::bi::link_mode + >::type type; +}; + +template +struct intrusive_tree_hook +{ + typedef typename container_detail::bi::make_bs_set_base_hook + < container_detail::bi::void_pointer + , container_detail::bi::link_mode + >::type type; +}; + +//This trait is used to type-pun std::pair because in C++03 +//compilers std::pair is useless for C++11 features +template +struct tree_internal_data_type +{ + typedef T type; +}; + +template +struct tree_internal_data_type< std::pair > +{ + typedef pair type; +}; + +//The node to be store in the tree +template +struct tree_node + : public intrusive_tree_hook::type +{ + private: + //BOOST_COPYABLE_AND_MOVABLE(tree_node) + tree_node(); + + public: + typedef typename intrusive_tree_hook + ::type hook_type; + typedef T value_type; + typedef typename tree_internal_data_type::type internal_type; + + typedef tree_node< T, VoidPointer + , tree_type_value, OptimizeSize> node_type; + + T &get_data() + { + T* ptr = reinterpret_cast(&this->m_data); + return *ptr; + } + + const T &get_data() const + { + const T* ptr = reinterpret_cast(&this->m_data); + return *ptr; + } + + internal_type m_data; + + template + void do_assign(const std::pair &p) + { + const_cast(m_data.first) = p.first; + m_data.second = p.second; + } + + template + void do_assign(const pair &p) + { + const_cast(m_data.first) = p.first; + m_data.second = p.second; + } + + template + void do_assign(const V &v) + { m_data = v; } + + template + void do_move_assign(std::pair &p) + { + const_cast(m_data.first) = ::boost::move(p.first); + m_data.second = ::boost::move(p.second); + } + + template + void do_move_assign(pair &p) + { + const_cast(m_data.first) = ::boost::move(p.first); + m_data.second = ::boost::move(p.second); + } + + template + void do_move_assign(V &v) + { m_data = ::boost::move(v); } +}; + +template +struct iiterator_node_value_type< tree_node > { + typedef T type; +}; + +template +class insert_equal_end_hint_functor +{ + Icont &icont_; + + public: + insert_equal_end_hint_functor(Icont &icont) + : icont_(icont) + {} + + void operator()(Node &n) + { this->icont_.insert_equal(this->icont_.cend(), n); } +}; + +template +class push_back_functor +{ + Icont &icont_; + + public: + push_back_functor(Icont &icont) + : icont_(icont) + {} + + void operator()(Node &n) + { this->icont_.push_back(n); } +}; + +}//namespace container_detail { + +namespace container_detail { + +template< class NodeType, class NodeCompareType + , class SizeType, class HookType + , boost::container::tree_type_enum tree_type_value> +struct intrusive_tree_dispatch; + +template +struct intrusive_tree_dispatch + +{ + typedef typename container_detail::bi::make_rbtree + + ,container_detail::bi::base_hook + ,container_detail::bi::constant_time_size + ,container_detail::bi::size_type + >::type type; +}; + +template +struct intrusive_tree_dispatch + +{ + typedef typename container_detail::bi::make_avltree + + ,container_detail::bi::base_hook + ,container_detail::bi::constant_time_size + ,container_detail::bi::size_type + >::type type; +}; + +template +struct intrusive_tree_dispatch + +{ + typedef typename container_detail::bi::make_sgtree + + ,container_detail::bi::base_hook + ,container_detail::bi::floating_point + ,container_detail::bi::size_type + >::type type; +}; + +template +struct intrusive_tree_dispatch + +{ + typedef typename container_detail::bi::make_splaytree + + ,container_detail::bi::base_hook + ,container_detail::bi::constant_time_size + ,container_detail::bi::size_type + >::type type; +}; + +template +struct intrusive_tree_type +{ + private: + typedef typename boost::container:: + allocator_traits::value_type value_type; + typedef typename boost::container:: + allocator_traits::void_pointer void_pointer; + typedef typename boost::container:: + allocator_traits::size_type size_type; + typedef typename container_detail::tree_node + < value_type, void_pointer + , tree_type_value, OptimizeSize> node_type; + typedef node_compare node_compare_type; + //Deducing the hook type from node_type (e.g. node_type::hook_type) would + //provoke an early instantiation of node_type that could ruin recursive + //tree definitions, so retype the complete type to avoid any problem. + typedef typename intrusive_tree_hook + ::type hook_type; + public: + typedef typename intrusive_tree_dispatch + < node_type, node_compare_type + , size_type, hook_type + , tree_type_value>::type type; +}; + +//Trait to detect manually rebalanceable tree types +template +struct is_manually_balanceable +{ static const bool value = true; }; + +template<> struct is_manually_balanceable +{ static const bool value = false; }; + +template<> struct is_manually_balanceable +{ static const bool value = false; }; + +//Proxy traits to implement different operations depending on the +//is_manually_balanceable<>::value +template< boost::container::tree_type_enum tree_type_value + , bool IsManuallyRebalanceable = is_manually_balanceable::value> +struct intrusive_tree_proxy +{ + template + static void rebalance(Icont &) {} +}; + +template +struct intrusive_tree_proxy +{ + template + static void rebalance(Icont &c) + { c.rebalance(); } +}; + +} //namespace container_detail { + +namespace container_detail { + +//This functor will be used with Intrusive clone functions to obtain +//already allocated nodes from a intrusive container instead of +//allocating new ones. When the intrusive container runs out of nodes +//the node holder is used instead. +template +class RecyclingCloner +{ + typedef typename AllocHolder::intrusive_container intrusive_container; + typedef typename AllocHolder::Node node_type; + typedef typename AllocHolder::NodePtr node_ptr_type; + + public: + RecyclingCloner(AllocHolder &holder, intrusive_container &itree) + : m_holder(holder), m_icont(itree) + {} + + static void do_assign(node_ptr_type &p, const node_type &other, bool_) + { p->do_assign(other.m_data); } + + static void do_assign(node_ptr_type &p, const node_type &other, bool_) + { p->do_move_assign(const_cast(other).m_data); } + + node_ptr_type operator()(const node_type &other) const + { + if(node_ptr_type p = m_icont.unlink_leftmost_without_rebalance()){ + //First recycle a node (this can't throw) + BOOST_TRY{ + //This can throw + this->do_assign(p, other, bool_()); + return p; + } + BOOST_CATCH(...){ + //If there is an exception destroy the whole source + m_holder.destroy_node(p); + while((p = m_icont.unlink_leftmost_without_rebalance())){ + m_holder.destroy_node(p); + } + BOOST_RETHROW + } + BOOST_CATCH_END + } + else{ + return m_holder.create_node(other.m_data); + } + } + + AllocHolder &m_holder; + intrusive_container &m_icont; +}; + +template +//where KeyValueCompare is tree_value_compare +struct key_node_compare + : private KeyValueCompare +{ + explicit key_node_compare(const KeyValueCompare &comp) + : KeyValueCompare(comp) + {} + + template + struct is_node + { + static const bool value = is_same::value; + }; + + template + typename enable_if_c::value, const typename KeyValueCompare::value_type &>::type + key_forward(const T &node) const + { return node.get_data(); } + + template + typename enable_if_c::value, const T &>::type + key_forward(const T &key) const + { return key; } + + template + bool operator()(const KeyType &key1, const KeyType2 &key2) const + { return KeyValueCompare::operator()(this->key_forward(key1), this->key_forward(key2)); } +}; + +template +class tree + : protected container_detail::node_alloc_holder + < A + , typename container_detail::intrusive_tree_type + < A, tree_value_compare //ValComp + , Options::tree_type, Options::optimize_size>::type + > +{ + typedef tree_value_compare + ValComp; + typedef typename container_detail::intrusive_tree_type + < A, ValComp, Options::tree_type + , Options::optimize_size>::type Icont; + typedef container_detail::node_alloc_holder + AllocHolder; + typedef typename AllocHolder::NodePtr NodePtr; + typedef tree < Key, Value, KeyOfValue + , KeyCompare, A, Options> ThisType; + typedef typename AllocHolder::NodeAlloc NodeAlloc; + typedef typename AllocHolder::ValAlloc ValAlloc; + typedef typename AllocHolder::Node Node; + typedef typename Icont::iterator iiterator; + typedef typename Icont::const_iterator iconst_iterator; + typedef container_detail::allocator_destroyer Destroyer; + typedef typename AllocHolder::allocator_v1 allocator_v1; + typedef typename AllocHolder::allocator_v2 allocator_v2; + typedef typename AllocHolder::alloc_version alloc_version; + typedef intrusive_tree_proxy intrusive_tree_proxy_t; + + BOOST_COPYABLE_AND_MOVABLE(tree) + + public: + + typedef Key key_type; + typedef Value value_type; + typedef A allocator_type; + typedef KeyCompare key_compare; + typedef ValComp value_compare; + typedef typename boost::container:: + allocator_traits::pointer pointer; + typedef typename boost::container:: + allocator_traits::const_pointer const_pointer; + typedef typename boost::container:: + allocator_traits::reference reference; + typedef typename boost::container:: + allocator_traits::const_reference const_reference; + typedef typename boost::container:: + allocator_traits::size_type size_type; + typedef typename boost::container:: + allocator_traits::difference_type difference_type; + typedef difference_type tree_difference_type; + typedef pointer tree_pointer; + typedef const_pointer tree_const_pointer; + typedef reference tree_reference; + typedef const_reference tree_const_reference; + typedef NodeAlloc stored_allocator_type; + + private: + + typedef key_node_compare KeyNodeCompare; + + public: + typedef container_detail::iterator iterator; + typedef container_detail::iterator const_iterator; + typedef container_detail::reverse_iterator reverse_iterator; + typedef container_detail::reverse_iterator const_reverse_iterator; + + tree() + : AllocHolder(ValComp(key_compare())) + {} + + explicit tree(const key_compare& comp, const allocator_type& a = allocator_type()) + : AllocHolder(a, ValComp(comp)) + {} + + explicit tree(const allocator_type& a) + : AllocHolder(a) + {} + + template + tree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp, + const allocator_type& a + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < container_detail::is_input_iterator::value + || container_detail::is_same::value + >::type * = 0 + #endif + ) + : AllocHolder(a, value_compare(comp)) + { + //Use cend() as hint to achieve linear time for + //ordered ranges as required by the standard + //for the constructor + const const_iterator end_it(this->cend()); + if(unique_insertion){ + for ( ; first != last; ++first){ + this->insert_unique(end_it, *first); + } + } + else{ + for ( ; first != last; ++first){ + this->insert_equal(end_it, *first); + } + } + } + + template + tree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp, + const allocator_type& a + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !(container_detail::is_input_iterator::value + || container_detail::is_same::value) + >::type * = 0 + #endif + ) + : AllocHolder(a, value_compare(comp)) + { + if(unique_insertion){ + //Use cend() as hint to achieve linear time for + //ordered ranges as required by the standard + //for the constructor + const const_iterator end_it(this->cend()); + for ( ; first != last; ++first){ + this->insert_unique(end_it, *first); + } + } + else{ + //Optimized allocation and construction + this->allocate_many_and_construct + ( first, std::distance(first, last) + , insert_equal_end_hint_functor(this->icont())); + } + } + + template + tree( ordered_range_t, InputIterator first, InputIterator last + , const key_compare& comp = key_compare(), const allocator_type& a = allocator_type() + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < container_detail::is_input_iterator::value + || container_detail::is_same::value + >::type * = 0 + #endif + ) + : AllocHolder(a, value_compare(comp)) + { + for ( ; first != last; ++first){ + this->push_back_impl(*first); + } + } + + template + tree( ordered_range_t, InputIterator first, InputIterator last + , const key_compare& comp = key_compare(), const allocator_type& a = allocator_type() + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !(container_detail::is_input_iterator::value + || container_detail::is_same::value) + >::type * = 0 + #endif + ) + : AllocHolder(a, value_compare(comp)) + { + //Optimized allocation and construction + this->allocate_many_and_construct + ( first, std::distance(first, last) + , container_detail::push_back_functor(this->icont())); + } + + tree(const tree& x) + : AllocHolder(x, x.value_comp()) + { + this->icont().clone_from + (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc())); + } + + tree(BOOST_RV_REF(tree) x) + : AllocHolder(::boost::move(static_cast(x)), x.value_comp()) + {} + + tree(const tree& x, const allocator_type &a) + : AllocHolder(a, x.value_comp()) + { + this->icont().clone_from + (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc())); + } + + tree(BOOST_RV_REF(tree) x, const allocator_type &a) + : AllocHolder(a, x.value_comp()) + { + if(this->node_alloc() == x.node_alloc()){ + this->icont().swap(x.icont()); + } + else{ + this->icont().clone_from + (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc())); + } + } + + ~tree() + {} //AllocHolder clears the tree + + tree& operator=(BOOST_COPY_ASSIGN_REF(tree) x) + { + if (&x != this){ + NodeAlloc &this_alloc = this->get_stored_allocator(); + const NodeAlloc &x_alloc = x.get_stored_allocator(); + container_detail::bool_:: + propagate_on_container_copy_assignment::value> flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + } + this->AllocHolder::copy_assign_alloc(x); + //Transfer all the nodes to a temporary tree + //If anything goes wrong, all the nodes will be destroyed + //automatically + Icont other_tree(::boost::move(this->icont())); + + //Now recreate the source tree reusing nodes stored by other_tree + this->icont().clone_from + (x.icont() + , RecyclingCloner(*this, other_tree) + , Destroyer(this->node_alloc())); + + //If there are remaining nodes, destroy them + NodePtr p; + while((p = other_tree.unlink_leftmost_without_rebalance())){ + AllocHolder::destroy_node(p); + } + } + return *this; + } + + tree& operator=(BOOST_RV_REF(tree) x) + { + BOOST_ASSERT(this != &x); + NodeAlloc &this_alloc = this->node_alloc(); + NodeAlloc &x_alloc = x.node_alloc(); + const bool propagate_alloc = allocator_traits:: + propagate_on_container_move_assignment::value; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy + this->clear(); + //Move allocator if needed + this->AllocHolder::move_assign_alloc(x); + //Obtain resources + this->icont() = boost::move(x.icont()); + } + //Else do a one by one move + else{ + //Transfer all the nodes to a temporary tree + //If anything goes wrong, all the nodes will be destroyed + //automatically + Icont other_tree(::boost::move(this->icont())); + + //Now recreate the source tree reusing nodes stored by other_tree + this->icont().clone_from + (x.icont() + , RecyclingCloner(*this, other_tree) + , Destroyer(this->node_alloc())); + + //If there are remaining nodes, destroy them + NodePtr p; + while((p = other_tree.unlink_leftmost_without_rebalance())){ + AllocHolder::destroy_node(p); + } + } + return *this; + } + + public: + // accessors: + value_compare value_comp() const + { return this->icont().value_comp().value_comp(); } + + key_compare key_comp() const + { return this->icont().value_comp().value_comp().key_comp(); } + + allocator_type get_allocator() const + { return allocator_type(this->node_alloc()); } + + const stored_allocator_type &get_stored_allocator() const + { return this->node_alloc(); } + + stored_allocator_type &get_stored_allocator() + { return this->node_alloc(); } + + iterator begin() + { return iterator(this->icont().begin()); } + + const_iterator begin() const + { return this->cbegin(); } + + iterator end() + { return iterator(this->icont().end()); } + + const_iterator end() const + { return this->cend(); } + + reverse_iterator rbegin() + { return reverse_iterator(end()); } + + const_reverse_iterator rbegin() const + { return this->crbegin(); } + + reverse_iterator rend() + { return reverse_iterator(begin()); } + + const_reverse_iterator rend() const + { return this->crend(); } + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const + { return const_iterator(this->non_const_icont().begin()); } + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const + { return const_iterator(this->non_const_icont().end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const + { return const_reverse_iterator(cend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const + { return const_reverse_iterator(cbegin()); } + + bool empty() const + { return !this->size(); } + + size_type size() const + { return this->icont().size(); } + + size_type max_size() const + { return AllocHolder::max_size(); } + + void swap(ThisType& x) + { AllocHolder::swap(x); } + + public: + + typedef typename Icont::insert_commit_data insert_commit_data; + + // insert/erase + std::pair insert_unique_check + (const key_type& key, insert_commit_data &data) + { + std::pair ret = + this->icont().insert_unique_check(key, KeyNodeCompare(value_comp()), data); + return std::pair(iterator(ret.first), ret.second); + } + + std::pair insert_unique_check + (const_iterator hint, const key_type& key, insert_commit_data &data) + { + std::pair ret = + this->icont().insert_unique_check(hint.get(), key, KeyNodeCompare(value_comp()), data); + return std::pair(iterator(ret.first), ret.second); + } + + iterator insert_unique_commit(const value_type& v, insert_commit_data &data) + { + NodePtr tmp = AllocHolder::create_node(v); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_unique_commit(*tmp, data)); + destroy_deallocator.release(); + return ret; + } + + template + iterator insert_unique_commit + (BOOST_FWD_REF(MovableConvertible) mv, insert_commit_data &data) + { + NodePtr tmp = AllocHolder::create_node(boost::forward(mv)); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_unique_commit(*tmp, data)); + destroy_deallocator.release(); + return ret; + } + + std::pair insert_unique(const value_type& v) + { + insert_commit_data data; + std::pair ret = + this->insert_unique_check(KeyOfValue()(v), data); + if(ret.second){ + ret.first = this->insert_unique_commit(v, data); + } + return ret; + } + + template + std::pair insert_unique(BOOST_FWD_REF(MovableConvertible) mv) + { + insert_commit_data data; + std::pair ret = + this->insert_unique_check(KeyOfValue()(mv), data); + if(ret.second){ + ret.first = this->insert_unique_commit(boost::forward(mv), data); + } + return ret; + } + + private: + + template + void push_back_impl(BOOST_FWD_REF(MovableConvertible) mv) + { + NodePtr tmp(AllocHolder::create_node(boost::forward(mv))); + //push_back has no-throw guarantee so avoid any deallocator/destroyer + this->icont().push_back(*tmp); + } + + std::pair emplace_unique_impl(NodePtr p) + { + value_type &v = p->get_data(); + insert_commit_data data; + scoped_destroy_deallocator destroy_deallocator(p, this->node_alloc()); + std::pair ret = + this->insert_unique_check(KeyOfValue()(v), data); + if(!ret.second){ + return ret; + } + //No throw insertion part, release rollback + destroy_deallocator.release(); + return std::pair + ( iterator(iiterator(this->icont().insert_unique_commit(*p, data))) + , true ); + } + + iterator emplace_unique_hint_impl(const_iterator hint, NodePtr p) + { + value_type &v = p->get_data(); + insert_commit_data data; + std::pair ret = + this->insert_unique_check(hint, KeyOfValue()(v), data); + if(!ret.second){ + Destroyer(this->node_alloc())(p); + return ret.first; + } + return iterator(iiterator(this->icont().insert_unique_commit(*p, data))); + } + + public: + + #ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + template + std::pair emplace_unique(Args&&... args) + { return this->emplace_unique_impl(AllocHolder::create_node(boost::forward(args)...)); } + + template + iterator emplace_hint_unique(const_iterator hint, Args&&... args) + { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(boost::forward(args)...)); } + + template + iterator emplace_equal(Args&&... args) + { + NodePtr tmp(AllocHolder::create_node(boost::forward(args)...)); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(this->icont().end(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + template + iterator emplace_hint_equal(const_iterator hint, Args&&... args) + { + NodePtr tmp(AllocHolder::create_node(boost::forward(args)...)); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(hint.get(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + return this->emplace_unique_impl \ + (AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint_unique(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + return this->emplace_unique_hint_impl \ + (hint, AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + NodePtr tmp(AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); \ + iterator ret(this->icont().insert_equal(this->icont().end(), *tmp)); \ + destroy_deallocator.release(); \ + return ret; \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint_equal(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + NodePtr tmp(AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); \ + iterator ret(this->icont().insert_equal(hint.get(), *tmp)); \ + destroy_deallocator.release(); \ + return ret; \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + iterator insert_unique(const_iterator hint, const value_type& v) + { + insert_commit_data data; + std::pair ret = + this->insert_unique_check(hint, KeyOfValue()(v), data); + if(!ret.second) + return ret.first; + return this->insert_unique_commit(v, data); + } + + template + iterator insert_unique(const_iterator hint, BOOST_FWD_REF(MovableConvertible) mv) + { + insert_commit_data data; + std::pair ret = + this->insert_unique_check(hint, KeyOfValue()(mv), data); + if(!ret.second) + return ret.first; + return this->insert_unique_commit(boost::forward(mv), data); + } + + template + void insert_unique(InputIterator first, InputIterator last) + { + for( ; first != last; ++first) + this->insert_unique(*first); + } + + iterator insert_equal(const value_type& v) + { + NodePtr tmp(AllocHolder::create_node(v)); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(this->icont().end(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + template + iterator insert_equal(BOOST_FWD_REF(MovableConvertible) mv) + { + NodePtr tmp(AllocHolder::create_node(boost::forward(mv))); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(this->icont().end(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + iterator insert_equal(const_iterator hint, const value_type& v) + { + NodePtr tmp(AllocHolder::create_node(v)); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(hint.get(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + template + iterator insert_equal(const_iterator hint, BOOST_FWD_REF(MovableConvertible) mv) + { + NodePtr tmp(AllocHolder::create_node(boost::forward(mv))); + scoped_destroy_deallocator destroy_deallocator(tmp, this->node_alloc()); + iterator ret(this->icont().insert_equal(hint.get(), *tmp)); + destroy_deallocator.release(); + return ret; + } + + template + void insert_equal(InputIterator first, InputIterator last) + { + for( ; first != last; ++first) + this->insert_equal(*first); + } + + iterator erase(const_iterator position) + { return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc()))); } + + size_type erase(const key_type& k) + { return AllocHolder::erase_key(k, KeyNodeCompare(value_comp()), alloc_version()); } + + iterator erase(const_iterator first, const_iterator last) + { return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version())); } + + void clear() + { AllocHolder::clear(alloc_version()); } + + // search operations. Const and non-const overloads even if no iterator is returned + // so splay implementations can to their rebalancing when searching in non-const versions + iterator find(const key_type& k) + { return iterator(this->icont().find(k, KeyNodeCompare(value_comp()))); } + + const_iterator find(const key_type& k) const + { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(value_comp()))); } + + size_type count(const key_type& k) const + { return size_type(this->icont().count(k, KeyNodeCompare(value_comp()))); } + + iterator lower_bound(const key_type& k) + { return iterator(this->icont().lower_bound(k, KeyNodeCompare(value_comp()))); } + + const_iterator lower_bound(const key_type& k) const + { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(value_comp()))); } + + iterator upper_bound(const key_type& k) + { return iterator(this->icont().upper_bound(k, KeyNodeCompare(value_comp()))); } + + const_iterator upper_bound(const key_type& k) const + { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(value_comp()))); } + + std::pair equal_range(const key_type& k) + { + std::pair ret = + this->icont().equal_range(k, KeyNodeCompare(value_comp())); + return std::pair(iterator(ret.first), iterator(ret.second)); + } + + std::pair equal_range(const key_type& k) const + { + std::pair ret = + this->non_const_icont().equal_range(k, KeyNodeCompare(value_comp())); + return std::pair + (const_iterator(ret.first), const_iterator(ret.second)); + } + + std::pair lower_bound_range(const key_type& k) + { + std::pair ret = + this->icont().lower_bound_range(k, KeyNodeCompare(value_comp())); + return std::pair(iterator(ret.first), iterator(ret.second)); + } + + std::pair lower_bound_range(const key_type& k) const + { + std::pair ret = + this->non_const_icont().lower_bound_range(k, KeyNodeCompare(value_comp())); + return std::pair + (const_iterator(ret.first), const_iterator(ret.second)); + } + + void rebalance() + { intrusive_tree_proxy_t::rebalance(this->icont()); } + + friend bool operator==(const tree& x, const tree& y) + { return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); } + + friend bool operator<(const tree& x, const tree& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + friend bool operator!=(const tree& x, const tree& y) + { return !(x == y); } + + friend bool operator>(const tree& x, const tree& y) + { return y < x; } + + friend bool operator<=(const tree& x, const tree& y) + { return !(y < x); } + + friend bool operator>=(const tree& x, const tree& y) + { return !(x < y); } + + friend void swap(tree& x, tree& y) + { x.swap(y); } +}; + +} //namespace container_detail { +} //namespace container { +/* +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move + > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; +*/ +} //namespace boost { + +#include + +#endif //BOOST_CONTAINER_TREE_HPP diff --git a/boost/container/detail/type_traits.hpp b/boost/container/detail/type_traits.hpp new file mode 100644 index 0000000..9ff3614 --- /dev/null +++ b/boost/container/detail/type_traits.hpp @@ -0,0 +1,237 @@ +////////////////////////////////////////////////////////////////////////////// +// (C) Copyright John Maddock 2000. +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +// The alignment_of implementation comes from John Maddock's boost::alignment_of code +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_TYPE_TRAITS_HPP +#define BOOST_CONTAINER_CONTAINER_DETAIL_TYPE_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +struct nat{}; + +template +struct LowPriorityConversion +{ + // Convertible from T with user-defined-conversion rank. + LowPriorityConversion(const U&) { } +}; + +//boost::alignment_of yields to 10K lines of preprocessed code, so we +//need an alternative +template struct alignment_of; + +template +struct alignment_of_hack +{ + char c; + T t; + alignment_of_hack(); +}; + +template +struct alignment_logic +{ + enum{ value = A < S ? A : S }; +}; + +template< typename T > +struct alignment_of +{ + enum{ value = alignment_logic + < sizeof(alignment_of_hack) - sizeof(T) + , sizeof(T)>::value }; +}; + +//This is not standard, but should work with all compilers +union max_align +{ + char char_; + short short_; + int int_; + long long_; + #ifdef BOOST_HAS_LONG_LONG + long long long_long_; + #endif + float float_; + double double_; + long double long_double_; + void * void_ptr_; +}; + +template +struct remove_reference +{ + typedef T type; +}; + +template +struct remove_reference +{ + typedef T type; +}; + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + +template +struct remove_reference +{ + typedef T type; +}; + +#else + +} // namespace container_detail { +} //namespace container { + +template +class rv; + +namespace container { +namespace container_detail { + +template +struct remove_reference< ::boost::rv > +{ + typedef T type; +}; + +#endif + +template +struct is_reference +{ + enum { value = false }; +}; + +template +struct is_reference +{ + enum { value = true }; +}; + +template +struct is_pointer +{ + enum { value = false }; +}; + +template +struct is_pointer +{ + enum { value = true }; +}; + +template +struct add_reference +{ + typedef T& type; +}; + +template +struct add_reference +{ + typedef T& type; +}; + +template<> +struct add_reference +{ + typedef nat &type; +}; + +template<> +struct add_reference +{ + typedef const nat &type; +}; + +template +struct add_const_reference +{ typedef const T &type; }; + +template +struct add_const_reference +{ typedef T& type; }; + +template +struct add_const +{ typedef const T type; }; + +template +struct is_same +{ + typedef char yes_type; + struct no_type + { + char padding[8]; + }; + + template + static yes_type is_same_tester(V*, V*); + static no_type is_same_tester(...); + + static T *t; + static U *u; + + static const bool value = sizeof(yes_type) == sizeof(is_same_tester(t,u)); +}; + +template +struct remove_const +{ + typedef T type; +}; + +template +struct remove_const< const T> +{ + typedef T type; +}; + +template +struct remove_ref_const +{ + typedef typename remove_const< typename remove_reference::type >::type type; +}; + +template +struct make_unsigned +{ + typedef T type; +}; + +template <> struct make_unsigned {}; +template <> struct make_unsigned {typedef unsigned char type;}; +template <> struct make_unsigned {typedef unsigned short type;}; +template <> struct make_unsigned {typedef unsigned int type;}; +template <> struct make_unsigned {typedef unsigned long type;}; +#ifdef BOOST_HAS_LONG_LONG +template <> struct make_unsigned {typedef unsigned long long type;}; +#endif + +} // namespace container_detail +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_CONTAINER_DETAIL_TYPE_TRAITS_HPP diff --git a/boost/container/detail/utilities.hpp b/boost/container/detail/utilities.hpp new file mode 100644 index 0000000..25f035d --- /dev/null +++ b/boost/container/detail/utilities.hpp @@ -0,0 +1,1267 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP +#define BOOST_CONTAINER_DETAIL_UTILITIES_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include //for ::memmove / ::memcpy +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include //std::distance + +namespace boost { +namespace container { + +////////////////////////////////////////////////////////////////////////////// +// +// swap +// +////////////////////////////////////////////////////////////////////////////// + +namespace container_swap { + +template::value > +struct has_member_swap +{ + static const bool value = boost::container::container_detail:: + has_member_function_callable_with_swap::value; +}; + +template +struct has_member_swap +{ + static const bool value = false; +}; + +} //namespace container_swap { + +template inline +typename container_detail::enable_if_c + ::value, void>::type +swap_dispatch(T &left, T &right) //swap using member swap +{ + left.swap(right); // may throw +} + +template inline +typename container_detail::enable_if_c + ::value/* && boost::has_move_emulation_enabled::value*/, void>::type + swap_dispatch(T &left, T &right) +{ + T temp(boost::move(left)); // may throw + left = boost::move(right); // may throw + right = boost::move(temp); // may throw +} +/* +template inline +typename container_detail::enable_if_c + ::value && !boost::has_move_emulation_enabled::value, void>::type + swap_dispatch(T &left, T &right) +{ + using std::swap; + swap(left, right); // may throw +} +*/ +namespace container_detail { + +template +inline T* addressof(T& obj) +{ + return static_cast( + static_cast( + const_cast( + &reinterpret_cast(obj) + ))); +} + +template +const T &max_value(const T &a, const T &b) +{ return a > b ? a : b; } + +template +const T &min_value(const T &a, const T &b) +{ return a < b ? a : b; } + +enum NextCapacityOption { NextCapacityDouble, NextCapacity60Percent }; + +template +struct next_capacity_calculator; + +template +struct next_capacity_calculator +{ + static SizeType get(const SizeType max_size + ,const SizeType capacity + ,const SizeType n) + { + const SizeType remaining = max_size - capacity; + if ( remaining < n ) + boost::container::throw_length_error("get_next_capacity, allocator's max_size reached"); + const SizeType additional = max_value(n, capacity); + return ( remaining < additional ) ? max_size : ( capacity + additional ); + } +}; + + +template +struct next_capacity_calculator +{ + static SizeType get(const SizeType max_size + ,const SizeType capacity + ,const SizeType n) + { + const SizeType remaining = max_size - capacity; + if ( remaining < n ) + boost::container::throw_length_error("get_next_capacity, allocator's max_size reached"); + const SizeType m3 = max_size/3; + + if (capacity < m3) + return capacity + max_value(3*(capacity+1)/5, n); + + if (capacity < m3*2) + return capacity + max_value((capacity+1)/2, n); + return max_size; + } +}; + +template +inline T* to_raw_pointer(T* p) +{ return p; } + +template +inline typename boost::intrusive::pointer_traits::element_type* + to_raw_pointer(const Pointer &p) +{ return boost::container::container_detail::to_raw_pointer(p.operator->()); } + +template +inline T* iterator_to_pointer(T* i) +{ return i; } + +template +inline typename std::iterator_traits::pointer + iterator_to_pointer(const Iterator &i) +{ return i.operator->(); } + +template +inline + typename boost::intrusive::pointer_traits + ::pointer>::element_type* + iterator_to_raw_pointer(const Iterator &i) +{ return (to_raw_pointer)((iterator_to_pointer)(i)); } + + +template +inline void swap_alloc(AllocatorType &, AllocatorType &, container_detail::false_type) + BOOST_CONTAINER_NOEXCEPT +{} + +template +inline void swap_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type) +{ boost::container::swap_dispatch(l, r); } + +template +inline void assign_alloc(AllocatorType &, const AllocatorType &, container_detail::false_type) + BOOST_CONTAINER_NOEXCEPT +{} + +template +inline void assign_alloc(AllocatorType &l, const AllocatorType &r, container_detail::true_type) +{ l = r; } + +template +inline void move_alloc(AllocatorType &, AllocatorType &, container_detail::false_type) + BOOST_CONTAINER_NOEXCEPT +{} + +template +inline void move_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type) +{ l = ::boost::move(r); } + +//Rounds "orig_size" by excess to round_to bytes +template +inline SizeType get_rounded_size(SizeType orig_size, SizeType round_to) +{ + return ((orig_size-1)/round_to+1)*round_to; +} + +template +struct ct_rounded_size +{ + enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo }; +}; + +template +struct are_elements_contiguous +{ + static const bool value = false; +}; + +///////////////////////// +// raw pointers +///////////////////////// + +template +struct are_elements_contiguous +{ + static const bool value = true; +}; + +///////////////////////// +// predeclarations +///////////////////////// + +#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +template +class vector_iterator; + +template +class vector_const_iterator; + +#endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +} //namespace container_detail { +} //namespace container { + +namespace interprocess { + +template +class offset_ptr; + +} //namespace interprocess { + +namespace container { + +namespace container_detail { + +///////////////////////// +//vector_[const_]iterator +///////////////////////// + +#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +template +struct are_elements_contiguous > +{ + static const bool value = true; +}; + +template +struct are_elements_contiguous > +{ + static const bool value = true; +}; + +#endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +///////////////////////// +// offset_ptr +///////////////////////// + +template +struct are_elements_contiguous< ::boost::interprocess::offset_ptr > +{ + static const bool value = true; +}; + +template +struct are_contiguous_and_same +{ + static const bool is_same_io = + is_same< typename remove_const< typename ::std::iterator_traits::value_type >::type + , typename ::std::iterator_traits::value_type + >::value; + static const bool value = is_same_io && + are_elements_contiguous::value && + are_elements_contiguous::value; +}; + +template +struct is_memtransfer_copy_assignable +{ + static const bool value = are_contiguous_and_same::value && + boost::has_trivial_assign< typename ::std::iterator_traits::value_type >::value; +}; + +template +struct is_memtransfer_copy_constructible +{ + static const bool value = are_contiguous_and_same::value && + boost::has_trivial_copy< typename ::std::iterator_traits::value_type >::value; +}; + +template +struct enable_if_memtransfer_copy_constructible + : public enable_if_c::value, R> +{}; + +template +struct disable_if_memtransfer_copy_constructible + : public enable_if_c::value, R> +{}; + +template +struct enable_if_memtransfer_copy_assignable + : public enable_if_c::value, R> +{}; + +template +struct disable_if_memtransfer_copy_assignable + : public enable_if_c::value, R> +{}; + +template + // F models ForwardIterator +inline F memmove(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT +{ + typedef typename std::iterator_traits::value_type value_type; + typename std::iterator_traits::difference_type n = std::distance(f, l); + ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); + std::advance(r, n); + return r; +} + +template + // F models ForwardIterator +F memmove_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ + typedef typename std::iterator_traits::value_type value_type; + ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); + std::advance(r, n); + return r; +} + +template + // F models ForwardIterator +I memmove_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ + typedef typename std::iterator_traits::value_type value_type; + ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); + std::advance(f, n); + return f; +} + +template + // F models ForwardIterator +I memmove_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT +{ + typedef typename std::iterator_traits::value_type value_type; + ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); + std::advance(f, n); + std::advance(r, n); + return f; +} + +template +struct is_memzero_initializable +{ + typedef typename ::std::iterator_traits::value_type value_type; + static const bool value = are_elements_contiguous::value && + ( ::boost::is_integral::value || ::boost::is_enum::value + #if defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL) + || ::boost::is_pointer::value + #endif + #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) + || ::boost::is_floating_point::value + #endif + #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) && defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL) + || ::boost::is_pod::value + #endif + ); +}; + +template +struct enable_if_memzero_initializable + : public enable_if_c::value, R> +{}; + +template +struct disable_if_memzero_initializable + : public enable_if_c::value, R> +{}; + +} //namespace container_detail { + + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_move_alloc +// +////////////////////////////////////////////////////////////////////////////// + + +//! Effects: +//! \code +//! for (; f != l; ++r, ++f) +//! allocator_traits::construct(a, &*r, boost::move(*f)); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc(A &a, I f, I l, F r) +{ + F back = r; + BOOST_TRY{ + while (f != l) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc(A &, I f, I l, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove(f, l, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_move_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r, boost::move(*f)); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc_n(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_move_alloc_n_source +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r, boost::move(*f)); +//! \endcode +//! +//! Returns: f (after incremented) +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc_n_source(A &a, I f, typename std::iterator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_move_alloc_n_source(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_copy_alloc +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; f != l; ++r, ++f) +//! allocator_traits::construct(a, &*r, *f); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc(A &a, I f, I l, F r) +{ + F back = r; + BOOST_TRY{ + while (f != l) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc(A &, I f, I l, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove(f, l, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_copy_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r, *f); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc_n(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_copy_alloc_n_source +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r, *f); +//! \endcode +//! +//! Returns: f (after incremented) +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc_n_source(A &a, I f, typename std::iterator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); + ++f; ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_constructible::type + uninitialized_copy_alloc_n_source(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_value_init_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline typename container_detail::disable_if_memzero_initializable::type + uninitialized_value_init_alloc_n(A &a, typename allocator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r)); + ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memzero_initializable::type + uninitialized_value_init_alloc_n(A &, typename allocator_traits::difference_type n, F r) +{ + typedef typename std::iterator_traits::value_type value_type; + ::memset((void*)container_detail::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n); + std::advance(r, n); + return r; +} + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_default_init_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline F uninitialized_default_init_alloc_n(A &a, typename allocator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), default_init); + ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_fill_alloc +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; f != l; ++r, ++f) +//! allocator_traits::construct(a, &*r, *f); +//! \endcode +//! +//! Returns: r +template + +inline void uninitialized_fill_alloc(A &a, F f, F l, const T &t) +{ + F back = f; + BOOST_TRY{ + while (f != l) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(f), t); + ++f; + } + } + BOOST_CATCH(...){ + for (; back != l; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END +} + + +////////////////////////////////////////////////////////////////////////////// +// +// uninitialized_fill_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +//! Effects: +//! \code +//! for (; n--; ++r, ++f) +//! allocator_traits::construct(a, &*r, v); +//! \endcode +//! +//! Returns: r +template + // F models ForwardIterator +inline F uninitialized_fill_alloc_n(A &a, const T &v, typename allocator_traits::difference_type n, F r) +{ + F back = r; + BOOST_TRY{ + while (n--) { + allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), v); + ++r; + } + } + BOOST_CATCH(...){ + for (; back != r; ++back){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + return r; +} + +////////////////////////////////////////////////////////////////////////////// +// +// copy +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + copy(I f, I l, F r) +{ + while (f != l) { + *r = *f; + ++f; ++r; + } + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + copy(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove(f, l, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// copy_n +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + copy_n(I f, typename std::iterator_traits::difference_type n, F r) +{ + while (n--) { + *r = *f; + ++f; ++r; + } + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + copy_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// copy_n_source +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + copy_n_source(I f, typename std::iterator_traits::difference_type n, F r) +{ + while (n--) { + *r = *f; + ++f; ++r; + } + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + copy_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// copy_n_source_dest +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + copy_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) +{ + while (n--) { + *r = *f; + ++f; ++r; + } + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + copy_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source_dest(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// move +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + move(I f, I l, F r) +{ + while (f != l) { + *r = ::boost::move(*f); + ++f; ++r; + } + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + move(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove(f, l, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// move_n +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + move_n(I f, typename std::iterator_traits::difference_type n, F r) +{ + while (n--) { + *r = ::boost::move(*f); + ++f; ++r; + } + return r; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + move_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// move_n_source +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + move_n_source(I f, typename std::iterator_traits::difference_type n, F r) +{ + while (n--) { + *r = ::boost::move(*f); + ++f; ++r; + } + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + move_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// move_n_source_dest +// +////////////////////////////////////////////////////////////////////////////// + +template + // F models ForwardIterator +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + move_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) +{ + while (n--) { + *r = ::boost::move(*f); + ++f; ++r; + } + return f; +} + +template + // F models ForwardIterator +inline typename container_detail::enable_if_memtransfer_copy_assignable::type + move_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT +{ return container_detail::memmove_n_source_dest(f, n, r); } + +////////////////////////////////////////////////////////////////////////////// +// +// destroy_n +// +////////////////////////////////////////////////////////////////////////////// + +template + // I models InputIterator +inline void destroy_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n + ,typename boost::container::container_detail::enable_if_c + < !boost::has_trivial_destructor::value_type>::value >::type* = 0) +{ + while(n--){ + allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(f++)); + } +} + +template + // I models InputIterator +inline void destroy_alloc_n(A &, I, typename std::iterator_traits::difference_type + ,typename boost::container::container_detail::enable_if_c + < boost::has_trivial_destructor::value_type>::value >::type* = 0) +{} + +////////////////////////////////////////////////////////////////////////////// +// +// deep_swap_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +template + +inline typename container_detail::disable_if_memtransfer_copy_assignable::type + deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i + , G large_range_f, typename allocator_traits::size_type n_j) +{ + typename allocator_traits::size_type n = 0; + for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){ + boost::container::swap_dispatch(*short_range_f, *large_range_f); + } + boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw + boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); +} + +static const std::size_t DeepSwapAllocNMaxStorage = std::size_t(1) << std::size_t(11); //2K bytes + +template + +inline typename container_detail::enable_if_c + < container_detail::is_memtransfer_copy_assignable::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false + , void>::type + deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i + , G large_range_f, typename allocator_traits::size_type n_j) +{ + typedef typename allocator_traits::value_type value_type; + typedef typename boost::aligned_storage + ::value>::type storage_type; + storage_type storage; + + const std::size_t n_i_bytes = sizeof(value_type)*n_i; + void *const large_ptr = static_cast(container_detail::iterator_to_raw_pointer(large_range_f)); + void *const short_ptr = static_cast(container_detail::iterator_to_raw_pointer(short_range_f)); + void *const stora_ptr = static_cast(container_detail::iterator_to_raw_pointer(storage)); + ::memcpy(stora_ptr, large_ptr, n_i_bytes); + ::memcpy(large_ptr, short_ptr, n_i_bytes); + ::memcpy(short_ptr, stora_ptr, n_i_bytes); + std::advance(large_range_f, n_i); + std::advance(short_range_f, n_i); + boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw + boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); +} + +template + +inline typename container_detail::enable_if_c + < container_detail::is_memtransfer_copy_assignable::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage) + , void>::type + deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i + , G large_range_f, typename allocator_traits::size_type n_j) +{ + typedef typename allocator_traits::value_type value_type; + typedef typename boost::aligned_storage + ::value>::type storage_type; + storage_type storage; + const std::size_t sizeof_storage = sizeof(storage); + + std::size_t n_i_bytes = sizeof(value_type)*n_i; + char *large_ptr = static_cast(static_cast(container_detail::iterator_to_raw_pointer(large_range_f))); + char *short_ptr = static_cast(static_cast(container_detail::iterator_to_raw_pointer(short_range_f))); + char *stora_ptr = static_cast(static_cast(&storage)); + + std::size_t szt_times = n_i_bytes/sizeof_storage; + const std::size_t szt_rem = n_i_bytes%sizeof_storage; + + //Loop unrolling using Duff's device, as it seems it helps on some architectures + const std::size_t Unroll = 4; + std::size_t n = (szt_times + (Unroll-1))/Unroll; + const std::size_t branch_number = (!szt_times)*Unroll + (szt_times % Unroll); + switch(branch_number){ + case 4: + break; + case 0: do{ + ::memcpy(stora_ptr, large_ptr, sizeof_storage); + ::memcpy(large_ptr, short_ptr, sizeof_storage); + ::memcpy(short_ptr, stora_ptr, sizeof_storage); + large_ptr += sizeof_storage; + short_ptr += sizeof_storage; + BOOST_CONTAINER_FALLTHOUGH + case 3: + ::memcpy(stora_ptr, large_ptr, sizeof_storage); + ::memcpy(large_ptr, short_ptr, sizeof_storage); + ::memcpy(short_ptr, stora_ptr, sizeof_storage); + large_ptr += sizeof_storage; + short_ptr += sizeof_storage; + BOOST_CONTAINER_FALLTHOUGH + case 2: + ::memcpy(stora_ptr, large_ptr, sizeof_storage); + ::memcpy(large_ptr, short_ptr, sizeof_storage); + ::memcpy(short_ptr, stora_ptr, sizeof_storage); + large_ptr += sizeof_storage; + short_ptr += sizeof_storage; + BOOST_CONTAINER_FALLTHOUGH + case 1: + ::memcpy(stora_ptr, large_ptr, sizeof_storage); + ::memcpy(large_ptr, short_ptr, sizeof_storage); + ::memcpy(short_ptr, stora_ptr, sizeof_storage); + large_ptr += sizeof_storage; + short_ptr += sizeof_storage; + } while(--n); + } + ::memcpy(stora_ptr, large_ptr, szt_rem); + ::memcpy(large_ptr, short_ptr, szt_rem); + ::memcpy(short_ptr, stora_ptr, szt_rem); + std::advance(large_range_f, n_i); + std::advance(short_range_f, n_i); + boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw + boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); +} + + +////////////////////////////////////////////////////////////////////////////// +// +// copy_assign_range_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +template + +void copy_assign_range_alloc_n( A &a, I inp_start, typename allocator_traits::size_type n_i + , O out_start, typename allocator_traits::size_type n_o ) +{ + if (n_o < n_i){ + inp_start = boost::container::copy_n_source_dest(inp_start, n_o, out_start); // may throw + boost::container::uninitialized_copy_alloc_n(a, inp_start, n_i - n_o, out_start);// may throw + } + else{ + out_start = boost::container::copy_n(inp_start, n_i, out_start); // may throw + boost::container::destroy_alloc_n(a, out_start, n_o - n_i); + } +} + +////////////////////////////////////////////////////////////////////////////// +// +// move_assign_range_alloc_n +// +////////////////////////////////////////////////////////////////////////////// + +template + +void move_assign_range_alloc_n( A &a, I inp_start, typename allocator_traits::size_type n_i + , O out_start, typename allocator_traits::size_type n_o ) +{ + if (n_o < n_i){ + inp_start = boost::container::move_n_source_dest(inp_start, n_o, out_start); // may throw + boost::container::uninitialized_move_alloc_n(a, inp_start, n_i - n_o, out_start); // may throw + } + else{ + out_start = boost::container::move_n(inp_start, n_i, out_start); // may throw + boost::container::destroy_alloc_n(a, out_start, n_o - n_i); + } +} + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP diff --git a/boost/container/detail/value_init.hpp b/boost/container/detail/value_init.hpp new file mode 100644 index 0000000..68f9678 --- /dev/null +++ b/boost/container/detail/value_init.hpp @@ -0,0 +1,45 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_VALUE_INIT_HPP +#define BOOST_CONTAINER_DETAIL_VALUE_INIT_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace container { +namespace container_detail { + +template +struct value_init +{ + value_init() + : m_t() + {} + + operator T &() { return m_t; } + + T m_t; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_VALUE_INIT_HPP diff --git a/boost/container/detail/variadic_templates_tools.hpp b/boost/container/detail/variadic_templates_tools.hpp new file mode 100644 index 0000000..b07fe30 --- /dev/null +++ b/boost/container/detail/variadic_templates_tools.hpp @@ -0,0 +1,154 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP +#define BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include //std::size_t + +namespace boost { +namespace container { +namespace container_detail { + +template +class tuple; + +template<> class tuple<> +{}; + +template +class tuple + : private tuple +{ + typedef tuple inherited; + + public: + tuple() { } + + // implicit copy-constructor is okay + // Construct tuple from separate arguments. + tuple(typename add_const_reference::type v, + typename add_const_reference::type... vtail) + : inherited(vtail...), m_head(v) + {} + + // Construct tuple from another tuple. + template + tuple(const tuple& other) + : m_head(other.head()), inherited(other.tail()) + {} + + template + tuple& operator=(const tuple& other) + { + m_head = other.head(); + tail() = other.tail(); + return this; + } + + typename add_reference::type head() { return m_head; } + typename add_reference::type head() const { return m_head; } + + inherited& tail() { return *this; } + const inherited& tail() const { return *this; } + + protected: + Head m_head; +}; + + +template +tuple tie_forward(Values&&... values) +{ return tuple(values...); } + +template +struct tuple_element; + +template +struct tuple_element > +{ + typedef typename tuple_element >::type type; +}; + +template +struct tuple_element<0, tuple > +{ + typedef Head type; +}; + +template +class get_impl; + +template +class get_impl > +{ + typedef typename tuple_element >::type Element; + typedef get_impl > Next; + + public: + typedef typename add_reference::type type; + typedef typename add_const_reference::type const_type; + static type get(tuple& t) { return Next::get(t.tail()); } + static const_type get(const tuple& t) { return Next::get(t.tail()); } +}; + +template +class get_impl<0, tuple > +{ + public: + typedef typename add_reference::type type; + typedef typename add_const_reference::type const_type; + static type get(tuple& t) { return t.head(); } + static const_type get(const tuple& t){ return t.head(); } +}; + +template +typename get_impl >::type get(tuple& t) +{ return get_impl >::get(t); } + +template +typename get_impl >::const_type get(const tuple& t) +{ return get_impl >::get(t); } + +//////////////////////////////////////////////////// +// Builds an index_tuple<0, 1, 2, ..., Num-1>, that will +// be used to "unpack" into comma-separated values +// in a function call. +//////////////////////////////////////////////////// + +template +struct index_tuple{}; + +template > +struct build_number_seq; + +template +struct build_number_seq > + : build_number_seq > +{}; + +template +struct build_number_seq<0, index_tuple > +{ typedef index_tuple type; }; + + +}}} //namespace boost { namespace container { namespace container_detail { + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP diff --git a/boost/container/detail/version_type.hpp b/boost/container/detail/version_type.hpp new file mode 100644 index 0000000..2eabc62 --- /dev/null +++ b/boost/container/detail/version_type.hpp @@ -0,0 +1,103 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +// +// This code comes from N1953 document by Howard E. Hinnant +// +////////////////////////////////////////////////////////////////////////////// + + +#ifndef BOOST_CONTAINER_DETAIL_VERSION_TYPE_HPP +#define BOOST_CONTAINER_DETAIL_VERSION_TYPE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include + +namespace boost{ +namespace container { +namespace container_detail { + +//using namespace boost; + +template +struct version_type + : public container_detail::integral_constant +{ + typedef T type; + + version_type(const version_type&); +}; + +namespace impl{ + +template , typename T::version>::value> +struct extract_version +{ + static const unsigned value = 1; +}; + +template +struct extract_version +{ + static const unsigned value = T::version::value; +}; + +template +struct has_version +{ + private: + struct two {char _[2];}; + template static two test(...); + template static char test(const typename U::version*); + public: + static const bool value = sizeof(test(0)) == 1; + void dummy(){} +}; + +template ::value> +struct version +{ + static const unsigned value = 1; +}; + +template +struct version +{ + static const unsigned value = extract_version::value; +}; + +} //namespace impl + +template +struct version + : public container_detail::integral_constant::value> +{}; + +template +struct is_version +{ + static const bool value = + is_same< typename version::type, integral_constant >::value; +}; + +} //namespace container_detail { +} //namespace container { +} //namespace boost{ + +#include + +#endif //#define BOOST_CONTAINER_DETAIL_VERSION_TYPE_HPP diff --git a/boost/container/detail/workaround.hpp b/boost/container/detail/workaround.hpp new file mode 100644 index 0000000..55ebe33 --- /dev/null +++ b/boost/container/detail/workaround.hpp @@ -0,0 +1,72 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_DETAIL_WORKAROUND_HPP +#define BOOST_CONTAINER_DETAIL_WORKAROUND_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)\ + && !defined(BOOST_INTERPROCESS_DISABLE_VARIADIC_TMPL) + #define BOOST_CONTAINER_PERFECT_FORWARDING +#endif + +#if defined(BOOST_NO_CXX11_NOEXCEPT) + #if defined(BOOST_MSVC) + #define BOOST_CONTAINER_NOEXCEPT throw() + #else + #define BOOST_CONTAINER_NOEXCEPT + #endif + #define BOOST_CONTAINER_NOEXCEPT_IF(x) +#else + #define BOOST_CONTAINER_NOEXCEPT noexcept + #define BOOST_CONTAINER_NOEXCEPT_IF(x) noexcept(x) +#endif + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && defined(__GXX_EXPERIMENTAL_CXX0X__)\ + && (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__ < 40700) + #define BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST +#endif + +#if !defined(BOOST_FALLTHOUGH) + #define BOOST_CONTAINER_FALLTHOUGH +#else + #define BOOST_CONTAINER_FALLTHOUGH BOOST_FALLTHOUGH; +#endif + +//Macros for documentation purposes. For code, expands to the argument +#define BOOST_CONTAINER_IMPDEF(TYPE) TYPE +#define BOOST_CONTAINER_SEEDOC(TYPE) TYPE + +//Macros for memset optimization. In most platforms +//memsetting pointers and floatings is safe and faster. +// +//If your platform does not offer these guarantees +//define these to value zero. +#ifndef BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_NOT_ZERO +#define BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO 1 +#endif + +#ifndef BOOST_CONTAINER_MEMZEROED_POINTER_IS_NOT_NULL +#define BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL +#endif + +#define BOOST_CONTAINER_DOC1ST(TYPE1, TYPE2) TYPE2 +#define BOOST_CONTAINER_I , +#define BOOST_CONTAINER_DOCIGN(T) T +#define BOOST_CONTAINER_DOCONLY(T) + +#include + +#endif //#ifndef BOOST_CONTAINER_DETAIL_WORKAROUND_HPP diff --git a/boost/container/flat_map.hpp b/boost/container/flat_map.hpp new file mode 100644 index 0000000..e7ff31a --- /dev/null +++ b/boost/container/flat_map.hpp @@ -0,0 +1,1902 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_FLAT_MAP_HPP +#define BOOST_CONTAINER_FLAT_MAP_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace container_detail{ + +template +static D &force(const S &s) +{ return *const_cast((reinterpret_cast(&s))); } + +template +static D force_copy(S s) +{ + D *vp = reinterpret_cast(&s); + return D(*vp); +} + +} //namespace container_detail{ + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A flat_map is a kind of associative container that supports unique keys (contains at +//! most one of each key value) and provides for fast retrieval of values of another +//! type T based on the keys. The flat_map class supports random-access iterators. +//! +//! A flat_map satisfies all of the requirements of a container and of a reversible +//! container and of an associative container. A flat_map also provides +//! most operations described for unique keys. For a +//! flat_map the key_type is Key and the value_type is std::pair +//! (unlike std::map which value_type is std::pair<const Key, T>). +//! +//! Compare is the ordering function for Keys (e.g. std::less). +//! +//! Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair >). +//! +//! flat_map is similar to std::map but it's implemented like an ordered vector. +//! This means that inserting a new element into a flat_map invalidates +//! previous iterators and references +//! +//! Erasing an element invalidates iterators and references +//! pointing to elements that come after (their keys are bigger) the erased element. +//! +//! This container provides random-access iterators. +//! +//! \tparam Key is the key_type of the map +//! \tparam Value is the mapped_type +//! \tparam Compare is the ordering function for Keys (e.g. std::less). +//! \tparam Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair > ). +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template , class Allocator = std::allocator< std::pair< Key, T> > > +#else +template +#endif +class flat_map +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(flat_map) + //This is the tree that we should store if pair was movable + typedef container_detail::flat_tree, + container_detail::select1st< std::pair >, + Compare, + Allocator> tree_t; + + //This is the real tree stored here. It's based on a movable pair + typedef container_detail::flat_tree, + container_detail::select1st >, + Compare, + typename allocator_traits::template portable_rebind_alloc + >::type> impl_tree_t; + impl_tree_t m_flat_tree; // flat tree representing flat_map + + typedef typename impl_tree_t::value_type impl_value_type; + typedef typename impl_tree_t::const_iterator impl_const_iterator; + typedef typename impl_tree_t::allocator_type impl_allocator_type; + typedef container_detail::flat_tree_value_compare + < Compare + , container_detail::select1st< std::pair > + , std::pair > value_compare_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::iterator iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::const_iterator const_iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::reverse_iterator reverse_iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::const_reverse_iterator const_reverse_iterator_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef T mapped_type; + typedef std::pair value_type; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::const_pointer const_pointer; + typedef typename boost::container::allocator_traits::reference reference; + typedef typename boost::container::allocator_traits::const_reference const_reference; + typedef typename boost::container::allocator_traits::size_type size_type; + typedef typename boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(Allocator) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare; + typedef Compare key_compare; + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(reverse_iterator_impl) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(const_reverse_iterator_impl) const_reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(impl_value_type) movable_value_type; + + public: + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty flat_map. + //! + //! Complexity: Constant. + flat_map() + : m_flat_tree() + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map using the specified + //! comparison object and allocator. + //! + //! Complexity: Constant. + explicit flat_map(const Compare& comp, const allocator_type& a = allocator_type()) + : m_flat_tree(comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map using the specified allocator. + //! + //! Complexity: Constant. + explicit flat_map(const allocator_type& a) + : m_flat_tree(container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + flat_map(InputIterator first, InputIterator last, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(true, first, last, comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + flat_map( ordered_unique_range_t, InputIterator first, InputIterator last + , const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, first, last, comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin() ,il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is last - first. + flat_map(std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(true, il.begin(), il.end(), comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + flat_map(ordered_unique_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + + //! Effects: Copy constructs a flat_map. + //! + //! Complexity: Linear in x.size(). + flat_map(const flat_map& x) + : m_flat_tree(x.m_flat_tree) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a flat_map. + //! Constructs *this using x's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: x is emptied. + flat_map(BOOST_RV_REF(flat_map) x) + : m_flat_tree(boost::move(x.m_flat_tree)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Copy constructs a flat_map using the specified allocator. + //! + //! Complexity: Linear in x.size(). + flat_map(const flat_map& x, const allocator_type &a) + : m_flat_tree(x.m_flat_tree, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a flat_map using the specified allocator. + //! Constructs *this using x's resources. + //! + //! Complexity: Constant if x.get_allocator() == a, linear otherwise. + flat_map(BOOST_RV_REF(flat_map) x, const allocator_type &a) + : m_flat_tree(boost::move(x.m_flat_tree), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + flat_map& operator=(BOOST_COPY_ASSIGN_REF(flat_map) x) + { m_flat_tree = x.m_flat_tree; return *this; } + + //! Effects: Move constructs a flat_map. + //! Constructs *this using x's resources. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + flat_map& operator=(BOOST_RV_REF(flat_map) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { m_flat_tree = boost::move(x.m_flat_tree); return *this; } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign elements from il to *this + flat_map& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Returns a copy of the Allocator that + //! was passed to the object's constructor. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.get_allocator()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force(m_flat_tree.get_stored_allocator()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force(m_flat_tree.get_stored_allocator()); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.begin()); } + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.begin()); } + + //! Effects: Returns an iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.end()); } + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.end()); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rbegin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rbegin()); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rend()); } + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.cbegin()); } + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.cend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.crbegin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.crend()); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the container contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.empty(); } + + //! Effects: Returns the number of the elements contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.size(); } + + //! Effects: Returns the largest possible size of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.max_size(); } + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.capacity(); } + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws or T's copy constructor throws. + //! + //! Note: If capacity() is less than "cnt", iterators and references to + //! to values might be invalidated. + void reserve(size_type cnt) + { m_flat_tree.reserve(cnt); } + + //! Effects: Tries to deallocate the excess of memory created + // with previous allocations. The size of the vector is unchanged + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { m_flat_tree.shrink_to_fit(); } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: If there is no key equivalent to x in the flat_map, inserts + //! value_type(x, T()) into the flat_map. + //! + //! Returns: Allocator reference to the mapped_type corresponding to x in *this. + //! + //! Complexity: Logarithmic. + mapped_type &operator[](const key_type& k); + + //! Effects: If there is no key equivalent to x in the flat_map, inserts + //! value_type(move(x), T()) into the flat_map (the key is move-constructed) + //! + //! Returns: Allocator reference to the mapped_type corresponding to x in *this. + //! + //! Complexity: Logarithmic. + mapped_type &operator[](key_type &&k) ; + + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH( operator[] , key_type, mapped_type&, this->priv_subscript) + #endif + + //! Returns: Allocator reference to the element whose key is equivalent to x. + //! + //! Throws: An exception object of type out_of_range if no such element is present. + //! + //! Complexity: logarithmic. + T& at(const key_type& k) + { + iterator i = this->find(k); + if(i == this->end()){ + throw_out_of_range("flat_map::at key not found"); + } + return i->second; + } + + //! Returns: Allocator reference to the element whose key is equivalent to x. + //! + //! Throws: An exception object of type out_of_range if no such element is present. + //! + //! Complexity: logarithmic. + const T& at(const key_type& k) const + { + const_iterator i = this->find(k); + if(i == this->end()){ + throw_out_of_range("flat_map::at key not found"); + } + return i->second; + } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object x of type T constructed with + //! std::forward(args)... if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + std::pair emplace(Args&&... args) + { return container_detail::force_copy< std::pair >(m_flat_tree.emplace_unique(boost::forward(args)...)); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace_hint(const_iterator hint, Args&&... args) + { + return container_detail::force_copy + (m_flat_tree.emplace_hint_unique( container_detail::force_copy(hint) + , boost::forward(args)...)); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return container_detail::force_copy< std::pair > \ + (m_flat_tree.emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return container_detail::force_copy(m_flat_tree.emplace_hint_unique \ + (container_detail::force_copy(hint) \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + //! Effects: Inserts x if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + std::pair insert(const value_type& x) + { return container_detail::force_copy >( + m_flat_tree.insert_unique(container_detail::force(x))); } + + //! Effects: Inserts a new value_type move constructed from the pair if and + //! only if there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + std::pair insert(BOOST_RV_REF(value_type) x) + { return container_detail::force_copy >( + m_flat_tree.insert_unique(boost::move(container_detail::force(x)))); } + + //! Effects: Inserts a new value_type move constructed from the pair if and + //! only if there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + std::pair insert(BOOST_RV_REF(movable_value_type) x) + { + return container_detail::force_copy > + (m_flat_tree.insert_unique(boost::move(x))); + } + + //! Effects: Inserts a copy of x in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, const value_type& x) + { + return container_detail::force_copy( + m_flat_tree.insert_unique( container_detail::force_copy(p) + , container_detail::force(x))); + } + + //! Effects: Inserts an element move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, BOOST_RV_REF(value_type) x) + { + return container_detail::force_copy + (m_flat_tree.insert_unique( container_detail::force_copy(p) + , boost::move(container_detail::force(x)))); + } + + //! Effects: Inserts an element move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, BOOST_RV_REF(movable_value_type) x) + { + return container_detail::force_copy( + m_flat_tree.insert_unique(container_detail::force_copy(p), boost::move(x))); + } + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + template + void insert(InputIterator first, InputIterator last) + { m_flat_tree.insert_unique(first, last); } + + //! Requires: first, last are not iterators into *this. + //! + //! Requires: [first ,last) must be ordered according to the predicate and must be + //! unique values. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + template + void insert(ordered_unique_range_t, InputIterator first, InputIterator last) + { m_flat_tree.insert_unique(ordered_unique_range, first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.first() to il.end()) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { m_flat_tree.insert_unique(il.begin(), il.end()); } + + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Effects: inserts each element from the range [il.begin(), il.end()) if and only + //! if there is no element with key equivalent to the key of that element. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + void insert(ordered_unique_range_t, std::initializer_list il) + { m_flat_tree.insert_unique(ordered_unique_range, il.begin(), il.end()); } +#endif + + //! Effects: Erases the element pointed to by p. + //! + //! Returns: Returns an iterator pointing to the element immediately + //! following q prior to the element being erased. If no such element exists, + //! returns end(). + //! + //! Complexity: Linear to the elements with keys bigger than p + //! + //! Note: Invalidates elements with keys + //! not less than the erased element. + iterator erase(const_iterator p) + { + return container_detail::force_copy + (m_flat_tree.erase(container_detail::force_copy(p))); + } + + //! Effects: Erases all elements in the container with key equivalent to x. + //! + //! Returns: Returns the number of erased elements. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + size_type erase(const key_type& x) + { return m_flat_tree.erase(x); } + + //! Effects: Erases all the elements in the range [first, last). + //! + //! Returns: Returns last. + //! + //! Complexity: size()*N where N is the distance from first to last. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + iterator erase(const_iterator first, const_iterator last) + { + return container_detail::force_copy( + m_flat_tree.erase( container_detail::force_copy(first) + , container_detail::force_copy(last))); + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(flat_map& x) + { m_flat_tree.swap(x.m_flat_tree); } + + //! Effects: erase(a.begin(),a.end()). + //! + //! Postcondition: size() == 0. + //! + //! Complexity: linear in size(). + void clear() BOOST_CONTAINER_NOEXCEPT + { m_flat_tree.clear(); } + + ////////////////////////////////////////////// + // + // observers + // + ////////////////////////////////////////////// + + //! Effects: Returns the comparison object out + //! of which a was constructed. + //! + //! Complexity: Constant. + key_compare key_comp() const + { return container_detail::force_copy(m_flat_tree.key_comp()); } + + //! Effects: Returns an object of value_compare constructed out + //! of the comparison object. + //! + //! Complexity: Constant. + value_compare value_comp() const + { return value_compare(container_detail::force_copy(m_flat_tree.key_comp())); } + + ////////////////////////////////////////////// + // + // map operations + // + ////////////////////////////////////////////// + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x) + { return container_detail::force_copy(m_flat_tree.find(x)); } + + //! Returns: Allocator const_iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic.s + const_iterator find(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.find(x)); } + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const + { return static_cast(m_flat_tree.find(x) != m_flat_tree.end()); } + + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x) + { return container_detail::force_copy(m_flat_tree.lower_bound(x)); } + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.lower_bound(x)); } + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x) + { return container_detail::force_copy(m_flat_tree.upper_bound(x)); } + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.upper_bound(x)); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) + { return container_detail::force_copy >(m_flat_tree.lower_bound_range(x)); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const + { return container_detail::force_copy >(m_flat_tree.lower_bound_range(x)); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const flat_map& x, const flat_map& y) + { return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const flat_map& x, const flat_map& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const flat_map& x, const flat_map& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const flat_map& x, const flat_map& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const flat_map& x, const flat_map& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const flat_map& x, const flat_map& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(flat_map& x, flat_map& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + mapped_type &priv_subscript(const key_type& k) + { + iterator i = lower_bound(k); + // i->first is greater than or equivalent to k. + if (i == end() || key_comp()(k, (*i).first)){ + container_detail::value_init m; + i = insert(i, impl_value_type(k, ::boost::move(m.m_t))); + } + return (*i).second; + } + mapped_type &priv_subscript(BOOST_RV_REF(key_type) mk) + { + key_type &k = mk; + iterator i = lower_bound(k); + // i->first is greater than or equivalent to k. + if (i == end() || key_comp()(k, (*i).first)){ + container_detail::value_init m; + i = insert(i, impl_value_type(boost::move(k), ::boost::move(m.m_t))); + } + return (*i).second; + } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A flat_multimap is a kind of associative container that supports equivalent keys +//! (possibly containing multiple copies of the same key value) and provides for +//! fast retrieval of values of another type T based on the keys. The flat_multimap +//! class supports random-access iterators. +//! +//! A flat_multimap satisfies all of the requirements of a container and of a reversible +//! container and of an associative container. For a +//! flat_multimap the key_type is Key and the value_type is std::pair +//! (unlike std::multimap which value_type is std::pair<const Key, T>). +//! +//! Compare is the ordering function for Keys (e.g. std::less). +//! +//! Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair >). +//! +//! flat_multimap is similar to std::multimap but it's implemented like an ordered vector. +//! This means that inserting a new element into a flat_map invalidates +//! previous iterators and references +//! +//! Erasing an element invalidates iterators and references +//! pointing to elements that come after (their keys are bigger) the erased element. +//! +//! This container provides random-access iterators. +//! +//! \tparam Key is the key_type of the map +//! \tparam Value is the mapped_type +//! \tparam Compare is the ordering function for Keys (e.g. std::less). +//! \tparam Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair > ). +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template , class Allocator = std::allocator< std::pair< Key, T> > > +#else +template +#endif +class flat_multimap +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(flat_multimap) + typedef container_detail::flat_tree, + container_detail::select1st< std::pair >, + Compare, + Allocator> tree_t; + //This is the real tree stored here. It's based on a movable pair + typedef container_detail::flat_tree, + container_detail::select1st >, + Compare, + typename allocator_traits::template portable_rebind_alloc + >::type> impl_tree_t; + impl_tree_t m_flat_tree; // flat tree representing flat_map + + typedef typename impl_tree_t::value_type impl_value_type; + typedef typename impl_tree_t::const_iterator impl_const_iterator; + typedef typename impl_tree_t::allocator_type impl_allocator_type; + typedef container_detail::flat_tree_value_compare + < Compare + , container_detail::select1st< std::pair > + , std::pair > value_compare_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::iterator iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::const_iterator const_iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::reverse_iterator reverse_iterator_impl; + typedef typename container_detail::get_flat_tree_iterators + ::pointer>::const_reverse_iterator const_reverse_iterator_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef T mapped_type; + typedef std::pair value_type; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::const_pointer const_pointer; + typedef typename boost::container::allocator_traits::reference reference; + typedef typename boost::container::allocator_traits::const_reference const_reference; + typedef typename boost::container::allocator_traits::size_type size_type; + typedef typename boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(Allocator) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare; + typedef Compare key_compare; + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(reverse_iterator_impl) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(const_reverse_iterator_impl) const_reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(impl_value_type) movable_value_type; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty flat_map. + //! + //! Complexity: Constant. + flat_multimap() + : m_flat_tree() + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified comparison + //! object and allocator. + //! + //! Complexity: Constant. + explicit flat_multimap(const Compare& comp, + const allocator_type& a = allocator_type()) + : m_flat_tree(comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified allocator. + //! + //! Complexity: Constant. + explicit flat_multimap(const allocator_type& a) + : m_flat_tree(container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified comparison object + //! and allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + flat_multimap(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(false, first, last, comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified comparison object and + //! allocator, and inserts elements from the ordered range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + flat_multimap(ordered_range_t, InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, first, last, comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is last - first. + flat_multimap(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : m_flat_tree(false, il.begin(), il.end(), comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified comparison object and + //! allocator, and inserts elements from the ordered range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + flat_multimap(ordered_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + + //! Effects: Copy constructs a flat_multimap. + //! + //! Complexity: Linear in x.size(). + flat_multimap(const flat_multimap& x) + : m_flat_tree(x.m_flat_tree) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a flat_multimap. Constructs *this using x's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: x is emptied. + flat_multimap(BOOST_RV_REF(flat_multimap) x) + : m_flat_tree(boost::move(x.m_flat_tree)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Copy constructs a flat_multimap using the specified allocator. + //! + //! Complexity: Linear in x.size(). + flat_multimap(const flat_multimap& x, const allocator_type &a) + : m_flat_tree(x.m_flat_tree, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a flat_multimap using the specified allocator. + //! Constructs *this using x's resources. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + flat_multimap(BOOST_RV_REF(flat_multimap) x, const allocator_type &a) + : m_flat_tree(boost::move(x.m_flat_tree), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + flat_multimap& operator=(BOOST_COPY_ASSIGN_REF(flat_multimap) x) + { m_flat_tree = x.m_flat_tree; return *this; } + + //! Effects: this->swap(x.get()). + //! + //! Complexity: Constant. + flat_multimap& operator=(BOOST_RV_REF(flat_multimap) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { m_flat_tree = boost::move(x.m_flat_tree); return *this; } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign content of il to *this + //! + //! Complexity: Linear in il.size(). + flat_multimap& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Returns a copy of the Allocator that + //! was passed to the object's constructor. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.get_allocator()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force(m_flat_tree.get_stored_allocator()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force(m_flat_tree.get_stored_allocator()); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.begin()); } + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.begin()); } + + //! Effects: Returns an iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.end()); } + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.end()); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rbegin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rbegin()); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.rend()); } + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.cbegin()); } + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.cend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.crbegin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::force_copy(m_flat_tree.crend()); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the container contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.empty(); } + + //! Effects: Returns the number of the elements contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.size(); } + + //! Effects: Returns the largest possible size of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.max_size(); } + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT + { return m_flat_tree.capacity(); } + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws or T's copy constructor throws. + //! + //! Note: If capacity() is less than "cnt", iterators and references to + //! to values might be invalidated. + void reserve(size_type cnt) + { m_flat_tree.reserve(cnt); } + + //! Effects: Tries to deallocate the excess of memory created + // with previous allocations. The size of the vector is unchanged + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { m_flat_tree.shrink_to_fit(); } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace(Args&&... args) + { return container_detail::force_copy(m_flat_tree.emplace_equal(boost::forward(args)...)); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant time if the value + //! is to be inserted before p) plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace_hint(const_iterator hint, Args&&... args) + { + return container_detail::force_copy(m_flat_tree.emplace_hint_equal + (container_detail::force_copy(hint), boost::forward(args)...)); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return container_detail::force_copy(m_flat_tree.emplace_equal \ + (BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator hint \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return container_detail::force_copy(m_flat_tree.emplace_hint_equal \ + (container_detail::force_copy(hint) \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + //! Effects: Inserts x and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const value_type& x) + { + return container_detail::force_copy( + m_flat_tree.insert_equal(container_detail::force(x))); + } + + //! Effects: Inserts a new value move-constructed from x and returns + //! the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(BOOST_RV_REF(value_type) x) + { return container_detail::force_copy(m_flat_tree.insert_equal(boost::move(x))); } + + //! Effects: Inserts a new value move-constructed from x and returns + //! the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(BOOST_RV_REF(impl_value_type) x) + { return container_detail::force_copy(m_flat_tree.insert_equal(boost::move(x))); } + + //! Effects: Inserts a copy of x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant time if the value + //! is to be inserted before p) plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, const value_type& x) + { + return container_detail::force_copy + (m_flat_tree.insert_equal( container_detail::force_copy(p) + , container_detail::force(x))); + } + + //! Effects: Inserts a value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant time if the value + //! is to be inserted before p) plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, BOOST_RV_REF(value_type) x) + { + return container_detail::force_copy + (m_flat_tree.insert_equal(container_detail::force_copy(p) + , boost::move(x))); + } + + //! Effects: Inserts a value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant time if the value + //! is to be inserted before p) plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, BOOST_RV_REF(impl_value_type) x) + { + return container_detail::force_copy( + m_flat_tree.insert_equal(container_detail::force_copy(p), boost::move(x))); + } + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + template + void insert(InputIterator first, InputIterator last) + { m_flat_tree.insert_equal(first, last); } + + //! Requires: first, last are not iterators into *this. + //! + //! Requires: [first ,last) must be ordered according to the predicate. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + template + void insert(ordered_range_t, InputIterator first, InputIterator last) + { m_flat_tree.insert_equal(ordered_range, first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { m_flat_tree.insert_equal(il.begin(), il.end()); } + + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate. + //! + //! Effects: inserts each element from the range [il.begin(), il.end()) if and only + //! if there is no element with key equivalent to the key of that element. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + void insert(ordered_range_t, std::initializer_list il) + { m_flat_tree.insert_equal(ordered_range, il.begin(), il.end()); } +#endif + + //! Effects: Erases the element pointed to by p. + //! + //! Returns: Returns an iterator pointing to the element immediately + //! following q prior to the element being erased. If no such element exists, + //! returns end(). + //! + //! Complexity: Linear to the elements with keys bigger than p + //! + //! Note: Invalidates elements with keys + //! not less than the erased element. + iterator erase(const_iterator p) + { + return container_detail::force_copy( + m_flat_tree.erase(container_detail::force_copy(p))); + } + + //! Effects: Erases all elements in the container with key equivalent to x. + //! + //! Returns: Returns the number of erased elements. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + size_type erase(const key_type& x) + { return m_flat_tree.erase(x); } + + //! Effects: Erases all the elements in the range [first, last). + //! + //! Returns: Returns last. + //! + //! Complexity: size()*N where N is the distance from first to last. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + iterator erase(const_iterator first, const_iterator last) + { + return container_detail::force_copy + (m_flat_tree.erase( container_detail::force_copy(first) + , container_detail::force_copy(last))); + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(flat_multimap& x) + { m_flat_tree.swap(x.m_flat_tree); } + + //! Effects: erase(a.begin(),a.end()). + //! + //! Postcondition: size() == 0. + //! + //! Complexity: linear in size(). + void clear() BOOST_CONTAINER_NOEXCEPT + { m_flat_tree.clear(); } + + ////////////////////////////////////////////// + // + // observers + // + ////////////////////////////////////////////// + + //! Effects: Returns the comparison object out + //! of which a was constructed. + //! + //! Complexity: Constant. + key_compare key_comp() const + { return container_detail::force_copy(m_flat_tree.key_comp()); } + + //! Effects: Returns an object of value_compare constructed out + //! of the comparison object. + //! + //! Complexity: Constant. + value_compare value_comp() const + { return value_compare(container_detail::force_copy(m_flat_tree.key_comp())); } + + ////////////////////////////////////////////// + // + // map operations + // + ////////////////////////////////////////////// + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x) + { return container_detail::force_copy(m_flat_tree.find(x)); } + + //! Returns: An const_iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + const_iterator find(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.find(x)); } + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const + { return m_flat_tree.count(x); } + + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x) + { return container_detail::force_copy(m_flat_tree.lower_bound(x)); } + + //! Returns: Allocator const iterator pointing to the first element with key + //! not less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.lower_bound(x)); } + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x) + {return container_detail::force_copy(m_flat_tree.upper_bound(x)); } + + //! Returns: Allocator const iterator pointing to the first element with key + //! not less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const + { return container_detail::force_copy(m_flat_tree.upper_bound(x)); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) + { return container_detail::force_copy >(m_flat_tree.equal_range(x)); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const + { return container_detail::force_copy >(m_flat_tree.equal_range(x)); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const flat_multimap& x, const flat_multimap& y) + { return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const flat_multimap& x, const flat_multimap& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const flat_multimap& x, const flat_multimap& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const flat_multimap& x, const flat_multimap& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const flat_multimap& x, const flat_multimap& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const flat_multimap& x, const flat_multimap& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(flat_multimap& x, flat_multimap& y) + { x.swap(y); } +}; + +}} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move< boost::container::flat_multimap > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +} //namespace boost { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +#endif /* BOOST_CONTAINER_FLAT_MAP_HPP */ diff --git a/boost/container/flat_set.hpp b/boost/container/flat_set.hpp new file mode 100644 index 0000000..1307f34 --- /dev/null +++ b/boost/container/flat_set.hpp @@ -0,0 +1,1250 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_FLAT_SET_HPP +#define BOOST_CONTAINER_FLAT_SET_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif +namespace boost { +namespace container { + +//! flat_set is a Sorted Associative Container that stores objects of type Key. +//! It is also a Unique Associative Container, meaning that no two elements are the same. +//! +//! flat_set is similar to std::set but it's implemented like an ordered vector. +//! This means that inserting a new element into a flat_set invalidates +//! previous iterators and references +//! +//! Erasing an element of a flat_set invalidates iterators and references +//! pointing to elements that come after (their keys are bigger) the erased element. +//! +//! This container provides random-access iterators. +//! +//! \tparam Key is the type to be inserted in the set, which is also the key_type +//! \tparam Compare is the comparison functor used to order keys +//! \tparam Allocator is the allocator to be used to allocate memory for this container +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template , class Allocator = std::allocator > +#else +template +#endif +class flat_set + ///@cond + : public container_detail::flat_tree, Compare, Allocator> + ///@endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(flat_set) + typedef container_detail::flat_tree, Compare, Allocator> base_t; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef Key value_type; + typedef Compare key_compare; + typedef Compare value_compare; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + + public: + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty container. + //! + //! Complexity: Constant. + explicit flat_set() + : base_t() + {} + + //! Effects: Constructs an empty container using the specified + //! comparison object and allocator. + //! + //! Complexity: Constant. + explicit flat_set(const Compare& comp, + const allocator_type& a = allocator_type()) + : base_t(comp, a) + {} + + //! Effects: Constructs an empty container using the specified allocator. + //! + //! Complexity: Constant. + explicit flat_set(const allocator_type& a) + : base_t(a) + {} + + //! Effects: Constructs an empty container using the specified comparison object and + //! allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + flat_set(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(true, first, last, comp, a) + {} + + //! Effects: Constructs an empty container using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + flat_set(ordered_unique_range_t, InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty container using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is il.begin() - il.end(). + flat_set(std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(true, il.begin(), il.end(), comp, a) + { + + } + + //! Effects: Constructs an empty container using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + flat_set(ordered_unique_range_t, std::initializer_list il, + const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + { + + } +#endif + + //! Effects: Copy constructs the container. + //! + //! Complexity: Linear in x.size(). + flat_set(const flat_set& x) + : base_t(static_cast(x)) + {} + + //! Effects: Move constructs thecontainer. Constructs *this using mx's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: mx is emptied. + flat_set(BOOST_RV_REF(flat_set) mx) + : base_t(boost::move(static_cast(mx))) + {} + + //! Effects: Copy constructs a container using the specified allocator. + //! + //! Complexity: Linear in x.size(). + flat_set(const flat_set& x, const allocator_type &a) + : base_t(static_cast(x), a) + {} + + //! Effects: Move constructs a container using the specified allocator. + //! Constructs *this using mx's resources. + //! + //! Complexity: Constant if a == mx.get_allocator(), linear otherwise + flat_set(BOOST_RV_REF(flat_set) mx, const allocator_type &a) + : base_t(boost::move(static_cast(mx)), a) + {} + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + flat_set& operator=(BOOST_COPY_ASSIGN_REF(flat_set) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + flat_set& operator=(BOOST_RV_REF(flat_set) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { return static_cast(this->base_t::operator=(boost::move(static_cast(x)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Copy all elements from il to *this. + //! + //! Complexity: Linear in il.size(). + flat_set& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + + #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + //! Effects: Returns a copy of the Allocator that + //! was passed to the object's constructor. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns an iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns an iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns true if the container contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the number of the elements contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the largest possible size of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws or Key's copy constructor throws. + //! + //! Note: If capacity() is less than "cnt", iterators and references to + //! to values might be invalidated. + void reserve(size_type cnt); + + //! Effects: Tries to deallocate the excess of memory created + // with previous allocations. The size of the vector is unchanged + //! + //! Throws: If memory allocation throws, or Key's copy constructor throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit(); + + #endif // #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object x of type Key constructed with + //! std::forward(args)... if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + std::pair emplace(Args&&... args) + { return this->base_t::emplace_unique(boost::forward(args)...); } + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_unique(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); }\ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_unique \ + (p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts x if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + std::pair insert(const value_type &x); + + //! Effects: Inserts a new value_type move constructed from the pair if and + //! only if there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + std::pair insert(value_type &&x); + #else + private: + typedef std::pair insert_return_pair; + public: + BOOST_MOVE_CONVERSION_AWARE_CATCH(insert, value_type, insert_return_pair, this->priv_insert) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, const value_type &x); + + //! Effects: Inserts an element move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, value_type, iterator, this->priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_unique(first, last); } + + //! Requires: first, last are not iterators into *this and + //! must be ordered according to the predicate and must be + //! unique values. + //! + //! Effects: inserts each element from the range [first,last) .This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: Non-standard extension. If an element is inserted it might invalidate elements. + template + void insert(ordered_unique_range_t, InputIterator first, InputIterator last) + { this->base_t::insert_unique(ordered_unique_range, first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { this->base_t::insert_unique(il.begin(), il.end()); } + + //! Requires: Range [il.begin(), il.end()) must be ordered according to the predicate + //! and must be unique values. + //! + //! Effects: inserts each element from the range [il.begin(), il.end()) .This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + //! search time plus N*size() insertion time. + //! + //! Note: Non-standard extension. If an element is inserted it might invalidate elements. + void insert(ordered_unique_range_t, std::initializer_list il) + { this->base_t::insert_unique(ordered_unique_range, il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Erases the element pointed to by p. + //! + //! Returns: Returns an iterator pointing to the element immediately + //! following q prior to the element being erased. If no such element exists, + //! returns end(). + //! + //! Complexity: Linear to the elements with keys bigger than p + //! + //! Note: Invalidates elements with keys + //! not less than the erased element. + iterator erase(const_iterator p); + + //! Effects: Erases all elements in the container with key equivalent to x. + //! + //! Returns: Returns the number of erased elements. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + size_type erase(const key_type& x); + + //! Effects: Erases all the elements in the range [first, last). + //! + //! Returns: Returns last. + //! + //! Complexity: size()*N where N is the distance from first to last. + //! + //! Complexity: Logarithmic search time plus erasure time + //! linear to the elements with bigger keys. + iterator erase(const_iterator first, const_iterator last); + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(flat_set& x); + + //! Effects: erase(a.begin(),a.end()). + //! + //! Postcondition: size() == 0. + //! + //! Complexity: linear in size(). + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the comparison object out + //! of which a was constructed. + //! + //! Complexity: Constant. + key_compare key_comp() const; + + //! Effects: Returns an object of value_compare constructed out + //! of the comparison object. + //! + //! Complexity: Constant. + value_compare value_comp() const; + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x); + + //! Returns: Allocator const_iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + const_iterator find(const key_type& x) const; + + #endif // #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const + { return static_cast(this->base_t::find(x) != this->base_t::cend()); } + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const; + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const; + + #endif // #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const + { return this->base_t::lower_bound_range(x); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) + { return this->base_t::lower_bound_range(x); } + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const flat_set& x, const flat_set& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const flat_set& x, const flat_set& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const flat_set& x, const flat_set& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const flat_set& x, const flat_set& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const flat_set& x, const flat_set& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const flat_set& x, const flat_set& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(flat_set& x, flat_set& y); + + #endif //#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + template + std::pair priv_insert(BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_unique(::boost::forward(x)); } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_unique(p, ::boost::forward(x)); } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value &&has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! flat_multiset is a Sorted Associative Container that stores objects of type Key. +//! +//! flat_multiset can store multiple copies of the same key value. +//! +//! flat_multiset is similar to std::multiset but it's implemented like an ordered vector. +//! This means that inserting a new element into a flat_multiset invalidates +//! previous iterators and references +//! +//! Erasing an element invalidates iterators and references +//! pointing to elements that come after (their keys are bigger) the erased element. +//! +//! This container provides random-access iterators. +//! +//! \tparam Key is the type to be inserted in the multiset, which is also the key_type +//! \tparam Compare is the comparison functor used to order keys +//! \tparam Allocator is the allocator to be used to allocate memory for this container +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template , class Allocator = std::allocator > +#else +template +#endif +class flat_multiset + ///@cond + : public container_detail::flat_tree, Compare, Allocator> + ///@endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(flat_multiset) + typedef container_detail::flat_tree, Compare, Allocator> base_t; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef Key value_type; + typedef Compare key_compare; + typedef Compare value_compare; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + + //! @copydoc ::boost::container::flat_set::flat_set() + explicit flat_multiset() + : base_t() + {} + + //! @copydoc ::boost::container::flat_set::flat_set(const Compare&, const allocator_type&) + explicit flat_multiset(const Compare& comp, + const allocator_type& a = allocator_type()) + : base_t(comp, a) + {} + + //! @copydoc ::boost::container::flat_set::flat_set(const allocator_type&) + explicit flat_multiset(const allocator_type& a) + : base_t(a) + {} + + //! @copydoc ::boost::container::flat_set::flat_set(InputIterator, InputIterator, const Compare& comp, const allocator_type&) + template + flat_multiset(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(false, first, last, comp, a) + {} + + //! Effects: Constructs an empty flat_multiset using the specified comparison object and + //! allocator, and inserts elements from the ordered range [first ,last ). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + flat_multiset(ordered_range_t, InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @copydoc ::boost::container::flat_set::flat_set(std::initializer_list, const Compare& comp, const allocator_type&) + flat_multiset(std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(false, il.begin(), il.end(), comp, a) + {} + + //! @copydoc ::boost::container::flat_set::flat_set(ordered_unique_range_t, std::initializer_list, const Compare& comp, const allocator_type&) + flat_multiset(ordered_unique_range_t, std::initializer_list il, + const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + {} +#endif + + //! @copydoc ::boost::container::flat_set::flat_set(const flat_set &) + flat_multiset(const flat_multiset& x) + : base_t(static_cast(x)) + {} + + //! @copydoc ::boost::container::flat_set(flat_set &&) + flat_multiset(BOOST_RV_REF(flat_multiset) mx) + : base_t(boost::move(static_cast(mx))) + {} + + //! @copydoc ::boost::container::flat_set(const flat_set &, const allocator_type &) + flat_multiset(const flat_multiset& x, const allocator_type &a) + : base_t(static_cast(x), a) + {} + + //! @copydoc ::boost::container::flat_set(flat_set &&, const allocator_type &) + flat_multiset(BOOST_RV_REF(flat_multiset) mx, const allocator_type &a) + : base_t(boost::move(static_cast(mx)), a) + {} + + //! @copydoc ::boost::container::flat_set::operator=(const flat_set &) + flat_multiset& operator=(BOOST_COPY_ASSIGN_REF(flat_multiset) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! @copydoc ::boost::container::flat_set::operator=(flat_set &&) + flat_multiset& operator=(BOOST_RV_REF(flat_multiset) mx) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { return static_cast(this->base_t::operator=(boost::move(static_cast(mx)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @copydoc ::boost::container::flat_set::operator=(std::initializer_list) + flat_multiset& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::flat_set::get_allocator() + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::get_stored_allocator() + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::get_stored_allocator() const + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::begin() + iterator begin() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::begin() const + const_iterator begin() const; + + //! @copydoc ::boost::container::flat_set::cbegin() const + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::end() + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::end() const + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::cend() const + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::rbegin() + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::rbegin() const + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::crbegin() const + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::rend() + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::rend() const + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::crend() const + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::empty() const + bool empty() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::size() const + size_type size() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::max_size() const + size_type max_size() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::capacity() const + size_type capacity() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::reserve(size_type) + void reserve(size_type cnt); + + //! @copydoc ::boost::container::flat_set::shrink_to_fit() + void shrink_to_fit(); + + #endif // #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace(Args&&... args) + { return this->base_t::emplace_equal(boost::forward(args)...); } + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_equal(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_equal \ + (p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts x and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const value_type &x); + + //! Effects: Inserts a new value_type move constructed from x + //! and returns the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic search time plus linear insertion + //! to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(insert, value_type, iterator, this->priv_insert) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, const value_type &x); + + //! Effects: Inserts a new value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic search time (constant if x is inserted + //! right before p) plus insertion linear to the elements with bigger keys than x. + //! + //! Note: If an element is inserted it might invalidate elements. + iterator insert(const_iterator p, value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, value_type, iterator, this->priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_equal(first, last); } + + //! Requires: first, last are not iterators into *this and + //! must be ordered according to the predicate. + //! + //! Effects: inserts each element from the range [first,last) .This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: Non-standard extension. If an element is inserted it might invalidate elements. + template + void insert(ordered_range_t, InputIterator first, InputIterator last) + { this->base_t::insert_equal(ordered_range, first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()). + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { this->base_t::insert_equal(il.begin(), il.end()); } + + //! Requires: Range [il.begin(), il.end()) must be ordered according to the predicate. + //! + //! Effects: inserts each element from the range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + //! search time plus N*size() insertion time. + //! + //! Note: Non-standard extension. If an element is inserted it might invalidate elements. + void insert(ordered_range_t, std::initializer_list il) + { this->base_t::insert_equal(ordered_range, il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::flat_set::erase(const_iterator) + iterator erase(const_iterator p); + + //! @copydoc ::boost::container::flat_set::erase(const key_type&) + size_type erase(const key_type& x); + + //! @copydoc ::boost::container::flat_set::erase(const_iterator,const_iterator) + iterator erase(const_iterator first, const_iterator last); + + //! @copydoc ::boost::container::flat_set::swap + void swap(flat_multiset& x); + + //! @copydoc ::boost::container::flat_set::clear + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::flat_set::key_comp + key_compare key_comp() const; + + //! @copydoc ::boost::container::flat_set::value_comp + value_compare value_comp() const; + + //! @copydoc ::boost::container::flat_set::find(const key_type& ) + iterator find(const key_type& x); + + //! @copydoc ::boost::container::flat_set::find(const key_type& ) const + const_iterator find(const key_type& x) const; + + //! @copydoc ::boost::container::flat_set::count(const key_type& ) const + size_type count(const key_type& x) const; + + //! @copydoc ::boost::container::flat_set::lower_bound(const key_type& ) + iterator lower_bound(const key_type& x); + + //! @copydoc ::boost::container::flat_set::lower_bound(const key_type& ) const + const_iterator lower_bound(const key_type& x) const; + + //! @copydoc ::boost::container::flat_set::upper_bound(const key_type& ) + iterator upper_bound(const key_type& x); + + //! @copydoc ::boost::container::flat_set::upper_bound(const key_type& ) const + const_iterator upper_bound(const key_type& x) const; + + //! @copydoc ::boost::container::flat_set::equal_range(const key_type& ) const + std::pair equal_range(const key_type& x) const; + + //! @copydoc ::boost::container::flat_set::equal_range(const key_type& ) + std::pair equal_range(const key_type& x); + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const flat_multiset& x, const flat_multiset& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const flat_multiset& x, const flat_multiset& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const flat_multiset& x, const flat_multiset& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const flat_multiset& x, const flat_multiset& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const flat_multiset& x, const flat_multiset& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const flat_multiset& x, const flat_multiset& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(flat_multiset& x, flat_multiset& y); + + #endif //#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + template + iterator priv_insert(BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_equal(::boost::forward(x)); } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_equal(p, ::boost::forward(x)); } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} + +#include + +#endif /* BOOST_CONTAINER_FLAT_SET_HPP */ diff --git a/boost/container/list.hpp b/boost/container/list.hpp new file mode 100644 index 0000000..33cc6ee --- /dev/null +++ b/boost/container/list.hpp @@ -0,0 +1,1462 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// + +#ifndef BOOST_CONTAINER_LIST_HPP +#define BOOST_CONTAINER_LIST_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) +#else +//Preprocessor library to emulate perfect forwarding +#include +#endif + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +namespace container_detail { + +template +struct list_hook +{ + typedef typename container_detail::bi::make_list_base_hook + , container_detail::bi::link_mode >::type type; +}; + +template +struct list_node + : public list_hook::type +{ + private: + list_node(); + + public: + typedef T value_type; + typedef typename list_hook::type hook_type; + + T m_data; + + T &get_data() + { return this->m_data; } + + const T &get_data() const + { return this->m_data; } +}; + +template +struct iiterator_node_value_type< list_node > { + typedef T type; +}; + +template +struct intrusive_list_type +{ + typedef boost::container::allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::value_type value_type; + typedef typename boost::intrusive::pointer_traits + ::template + rebind_pointer::type + void_pointer; + typedef typename container_detail::list_node + node_type; + typedef typename container_detail::bi::make_list + < node_type + , container_detail::bi::base_hook::type> + , container_detail::bi::constant_time_size + , container_detail::bi::size_type + + >::type container_type; + typedef container_type type ; +}; + +} //namespace container_detail { +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A list is a doubly linked list. That is, it is a Sequence that supports both +//! forward and backward traversal, and (amortized) constant time insertion and +//! removal of elements at the beginning or the end, or in the middle. Lists have +//! the important property that insertion and splicing do not invalidate iterators +//! to list elements, and that even removal invalidates only the iterators that point +//! to the elements that are removed. The ordering of iterators may be changed +//! (that is, list::iterator might have a different predecessor or successor +//! after a list operation than it did before), but the iterators themselves will +//! not be invalidated or made to point to different elements unless that invalidation +//! or mutation is explicit. +//! +//! \tparam T The type of object that is stored in the list +//! \tparam Allocator The allocator used for all internal memory management +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template > +#else +template +#endif +class list + : protected container_detail::node_alloc_holder + ::type> +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef typename + container_detail::intrusive_list_type::type Icont; + typedef container_detail::node_alloc_holder AllocHolder; + typedef typename AllocHolder::NodePtr NodePtr; + typedef typename AllocHolder::NodeAlloc NodeAlloc; + typedef typename AllocHolder::ValAlloc ValAlloc; + typedef typename AllocHolder::Node Node; + typedef container_detail::allocator_destroyer Destroyer; + typedef typename AllocHolder::allocator_v1 allocator_v1; + typedef typename AllocHolder::allocator_v2 allocator_v2; + typedef typename AllocHolder::alloc_version alloc_version; + typedef boost::container::allocator_traits allocator_traits_type; + + class equal_to_value + { + typedef typename AllocHolder::value_type value_type; + const value_type &t_; + + public: + equal_to_value(const value_type &t) + : t_(t) + {} + + bool operator()(const value_type &t)const + { return t_ == t; } + }; + + template + struct ValueCompareToNodeCompare + : Pred + { + ValueCompareToNodeCompare(Pred pred) + : Pred(pred) + {} + + bool operator()(const Node &a, const Node &b) const + { return static_cast(*this)(a.m_data, b.m_data); } + + bool operator()(const Node &a) const + { return static_cast(*this)(a.m_data); } + }; + + BOOST_COPYABLE_AND_MOVABLE(list) + + typedef container_detail::iterator iterator_impl; + typedef container_detail::iterator const_iterator_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef T value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(NodeAlloc) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) const_reverse_iterator; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs a list. + //! + //! Throws: If allocator_type's default constructor throws. + //! + //! Complexity: Constant. + list() + : AllocHolder() + {} + + //! Effects: Constructs a list taking the allocator as parameter. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + explicit list(const allocator_type &a) BOOST_CONTAINER_NOEXCEPT + : AllocHolder(a) + {} + + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + explicit list(size_type n) + : AllocHolder(Allocator()) + { this->resize(n); } + + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + list(size_type n, const T& value, const Allocator& a = Allocator()) + : AllocHolder(a) + { this->insert(this->cbegin(), n, value); } + + //! Effects: Copy constructs a list. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor throws. + //! + //! Complexity: Linear to the elements x contains. + list(const list& x) + : AllocHolder(x) + { this->insert(this->cbegin(), x.begin(), x.end()); } + + //! Effects: Move constructor. Moves mx's resources to *this. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant. + list(BOOST_RV_REF(list) x) + : AllocHolder(boost::move(static_cast(x))) + {} + + //! Effects: Copy constructs a list using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor or copy constructor throws. + //! + //! Complexity: Linear to the elements x contains. + list(const list& x, const allocator_type &a) + : AllocHolder(a) + { this->insert(this->cbegin(), x.begin(), x.end()); } + + //! Effects: Move constructor sing the specified allocator. + //! Moves mx's resources to *this. + //! + //! Throws: If allocation or value_type's copy constructor throws. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + list(BOOST_RV_REF(list) x, const allocator_type &a) + : AllocHolder(a) + { + if(this->node_alloc() == x.node_alloc()){ + this->icont().swap(x.icont()); + } + else{ + this->insert(this->cbegin(), x.begin(), x.end()); + } + } + + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts a copy of the range [first, last) in the list. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + list(InpIt first, InpIt last, const Allocator &a = Allocator()) + : AllocHolder(a) + { this->insert(this->cbegin(), first, last); } + + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.end()) in the list. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced + //! std::initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + list(std::initializer_list il, const Allocator &a = Allocator()) + : AllocHolder(a) + { this->insert(this->cbegin(), il.begin(), il.end()); } +#endif + + //! Effects: Destroys the list. All stored values are destroyed + //! and used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements. + ~list() BOOST_CONTAINER_NOEXCEPT + {} //AllocHolder clears the list + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in x. + list& operator=(BOOST_COPY_ASSIGN_REF(list) x) + { + if (&x != this){ + NodeAlloc &this_alloc = this->node_alloc(); + const NodeAlloc &x_alloc = x.node_alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + } + this->AllocHolder::copy_assign_alloc(x); + this->assign(x.begin(), x.end()); + } + return *this; + } + + //! Effects: Move assignment. All x's values are transferred to *this. + //! + //! Postcondition: x.empty(). *this contains a the elements x had + //! before the function. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + list& operator=(BOOST_RV_REF(list) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + BOOST_ASSERT(this != &x); + NodeAlloc &this_alloc = this->node_alloc(); + NodeAlloc &x_alloc = x.node_alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy + this->clear(); + //Move allocator if needed + this->AllocHolder::move_assign_alloc(x); + //Obtain resources + this->icont() = boost::move(x.icont()); + } + //Else do a one by one move + else{ + this->assign( boost::make_move_iterator(x.begin()) + , boost::make_move_iterator(x.end())); + } + return *this; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Makes *this contain the same elements as il. + //! + //! Postcondition: this->size() == il.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in x. + list& operator=(std::initializer_list il) + { + assign(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Assigns the n copies of val to *this. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + void assign(size_type n, const T& val) + { + typedef constant_iterator cvalue_iterator; + return this->assign(cvalue_iterator(val, n), cvalue_iterator()); + } + + //! Effects: Assigns the the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing InpIt throws. + //! + //! Complexity: Linear to n. + template + void assign(InpIt first, InpIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + >::type * = 0 + #endif + ) + { + iterator first1 = this->begin(); + const iterator last1 = this->end(); + for ( ; first1 != last1 && first != last; ++first1, ++first) + *first1 = *first; + if (first == last) + this->erase(first1, last1); + else{ + this->insert(last1, first, last); + } + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing std::initializer_list iterator throws. + //! + //! Complexity: Linear to n. + void assign(std::initializer_list il) + { assign(il.begin(), il.end()); } +#endif + + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return allocator_type(this->node_alloc()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return this->node_alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->node_alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().begin()); } + + //! Effects: Returns a const_iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return this->cbegin(); } + + //! Effects: Returns an iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().end()); } + + //! Effects: Returns a const_iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return this->cend(); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->crbegin(); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(begin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return this->crend(); } + + //! Effects: Returns a const_iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->non_const_icont().begin()); } + + //! Effects: Returns a const_iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->non_const_icont().end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->cend()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->cbegin()); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the list contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return !this->size(); } + + //! Effects: Returns the number of the elements contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return this->icont().size(); } + + //! Effects: Returns the largest possible size of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return AllocHolder::max_size(); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size) + { + if(!priv_try_shrink(new_size)){ + typedef value_init_construct_iterator value_init_iterator; + this->insert(this->cend(), value_init_iterator(new_size - this->size()), value_init_iterator()); + } + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size, const T& x) + { + if(!priv_try_shrink(new_size)){ + this->insert(this->cend(), new_size - this->size(), x); + } + } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() BOOST_CONTAINER_NOEXCEPT + { return *this->begin(); } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const BOOST_CONTAINER_NOEXCEPT + { return *this->begin(); } + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference back() BOOST_CONTAINER_NOEXCEPT + { return *(--this->end()); } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference back() const BOOST_CONTAINER_NOEXCEPT + { return *(--this->end()); } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the end of the list. + //! + //! Throws: If memory allocation throws or + //! T's in-place constructor throws. + //! + //! Complexity: Constant + template + void emplace_back(Args&&... args) + { this->emplace(this->cend(), boost::forward(args)...); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the beginning of the list. + //! + //! Throws: If memory allocation throws or + //! T's in-place constructor throws. + //! + //! Complexity: Constant + template + void emplace_front(Args&&... args) + { this->emplace(this->cbegin(), boost::forward(args)...); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... before p. + //! + //! Throws: If memory allocation throws or + //! T's in-place constructor throws. + //! + //! Complexity: Constant + template + iterator emplace(const_iterator p, Args&&... args) + { + NodePtr pnode(AllocHolder::create_node(boost::forward(args)...)); + return iterator(this->icont().insert(p.get(), *pnode)); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + this->emplace(this->cend() \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_front(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + this->emplace(this->cbegin() \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + NodePtr pnode (AllocHolder::create_node \ + (BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + return iterator(this->icont().insert(p.get(), *pnode)); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the beginning of the list. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_front(const T &x); + + //! Effects: Constructs a new element in the beginning of the list + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_front(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the end of the list. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_back(const T &x); + + //! Effects: Constructs a new element in the end of the list + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_back(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of x before p. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws or x's copy constructor throws. + //! + //! Complexity: Amortized constant time. + iterator insert(const_iterator p, const T &x); + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a new element before p with mx's resources. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + iterator insert(const_iterator p, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Inserts n copies of x before p. + //! + //! Returns: an iterator to the first inserted element or p if n is 0. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + iterator insert(const_iterator p, size_type n, const T& x) + { + typedef constant_iterator cvalue_iterator; + return this->insert(p, cvalue_iterator(x, n), cvalue_iterator()); + } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [first, last) range before p. + //! + //! Returns: an iterator to the first inserted element or p if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws. + //! + //! Complexity: Linear to std::distance [first, last). + template + iterator insert(const_iterator p, InpIt first, InpIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && (container_detail::is_input_iterator::value + || container_detail::is_same::value + ) + >::type * = 0 + #endif + ) + { + const typename Icont::iterator ipos(p.get()); + iterator ret_it(ipos); + if(first != last){ + ret_it = iterator(this->icont().insert(ipos, *this->create_node_from_it(first))); + ++first; + } + for (; first != last; ++first){ + this->icont().insert(ipos, *this->create_node_from_it(first)); + } + return ret_it; + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator p, FwdIt first, FwdIt last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !(container_detail::is_input_iterator::value + || container_detail::is_same::value + ) + >::type * = 0 + ) + { + //Optimized allocation and construction + insertion_functor func(this->icont(), p.get()); + iterator before_p(p.get()); + --before_p; + this->allocate_many_and_construct(first, std::distance(first, last), func); + return ++before_p; + } + #endif + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before p. + //! + //! Returns: an iterator to the first inserted element or p if if.begin() == il.end(). + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced std::initializer_list iterator throws. + //! + //! Complexity: Linear to std::distance [il.begin(), il.end()). + iterator insert(const_iterator p, std::initializer_list il) + { return insert(p, il.begin(), il.end()); } +#endif + + //! Effects: Removes the first element from the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Amortized constant time. + void pop_front() BOOST_CONTAINER_NOEXCEPT + { this->erase(this->cbegin()); } + + //! Effects: Removes the last element from the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Amortized constant time. + void pop_back() BOOST_CONTAINER_NOEXCEPT + { const_iterator tmp = this->cend(); this->erase(--tmp); } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Erases the element at p p. + //! + //! Throws: Nothing. + //! + //! Complexity: Amortized constant time. + iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().erase_and_dispose(p.get(), Destroyer(this->node_alloc()))); } + + //! Requires: first and last must be valid iterator to elements in *this. + //! + //! Effects: Erases the elements pointed by [first, last). + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the distance between first and last. + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version())); } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(list& x) + { AllocHolder::swap(x); } + + //! Effects: Erases all the elements of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the list. + void clear() BOOST_CONTAINER_NOEXCEPT + { AllocHolder::clear(alloc_version()); } + + ////////////////////////////////////////////// + // + // slist operations + // + ////////////////////////////////////////////// + + //! Requires: p must point to an element contained + //! by the list. x != *this. this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers all the elements of list x to this list, before the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, list& x) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this != &x); + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice(p.get(), x.icont()); + } + + //! Requires: p must point to an element contained + //! by the list. x != *this. this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers all the elements of list x to this list, before the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(list) x) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x)); } + + //! Requires: p must point to an element contained + //! by this list. i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! If p == i or p == ++i, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, list &x, const_iterator i) BOOST_CONTAINER_NOEXCEPT + { + //BOOST_ASSERT(this != &x); + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice(p.get(), x.icont(), i.get()); + } + + //! Requires: p must point to an element contained + //! by this list. i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! If p == i or p == ++i, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator i) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x), i); } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Linear to the number of elements transferred. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, list &x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice(p.get(), x.icont(), first.get(), last.get()); + } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Linear to the number of elements transferred. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x), first, last); } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! n == std::distance(first, last). this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + //! + //! Note: Non-standard extension + void splice(const_iterator p, list &x, const_iterator first, const_iterator last, size_type n) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice(p.get(), x.icont(), first.get(), last.get(), n); + } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! n == std::distance(first, last). this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + //! + //! Note: Non-standard extension + void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last, size_type n) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x), first, last, n); } + + //! Effects: Removes all the elements that compare equal to value. + //! + //! Throws: If comparison throws. + //! + //! Complexity: Linear time. It performs exactly size() comparisons for equality. + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + void remove(const T& value) + { this->remove_if(equal_to_value(value)); } + + //! Effects: Removes all the elements for which a specified + //! predicate is satisfied. + //! + //! Throws: If pred throws. + //! + //! Complexity: Linear time. It performs exactly size() calls to the predicate. + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + template + void remove_if(Pred pred) + { + typedef ValueCompareToNodeCompare Predicate; + this->icont().remove_and_dispose_if(Predicate(pred), Destroyer(this->node_alloc())); + } + + //! Effects: Removes adjacent duplicate elements or adjacent + //! elements that are equal from the list. + //! + //! Throws: If comparison throws. + //! + //! Complexity: Linear time (size()-1 comparisons equality comparisons). + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + void unique() + { this->unique(value_equal()); } + + //! Effects: Removes adjacent duplicate elements or adjacent + //! elements that satisfy some binary predicate from the list. + //! + //! Throws: If pred throws. + //! + //! Complexity: Linear time (size()-1 comparisons calls to pred()). + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + template + void unique(BinaryPredicate binary_pred) + { + typedef ValueCompareToNodeCompare Predicate; + this->icont().unique_and_dispose(Predicate(binary_pred), Destroyer(this->node_alloc())); + } + + //! Requires: The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this according to std::less. The merge is stable; + //! that is, if an element from *this is equivalent to one from x, then the element + //! from *this will precede the one from x. + //! + //! Throws: If comparison throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + void merge(list &x) + { this->merge(x, value_less()); } + + //! Requires: The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this according to std::less. The merge is stable; + //! that is, if an element from *this is equivalent to one from x, then the element + //! from *this will precede the one from x. + //! + //! Throws: If comparison throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + void merge(BOOST_RV_REF(list) x) + { this->merge(static_cast(x)); } + + //! Requires: p must be a comparison function that induces a strict weak + //! ordering and both *this and x must be sorted according to that ordering + //! The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this. The merge is stable; that is, if an element from *this is + //! equivalent to one from x, then the element from *this will precede the one from x. + //! + //! Throws: If comp throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + //! + //! Note: Iterators and references to *this are not invalidated. + template + void merge(list &x, const StrictWeakOrdering &comp) + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().merge(x.icont(), + ValueCompareToNodeCompare(comp)); + } + + //! Requires: p must be a comparison function that induces a strict weak + //! ordering and both *this and x must be sorted according to that ordering + //! The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this. The merge is stable; that is, if an element from *this is + //! equivalent to one from x, then the element from *this will precede the one from x. + //! + //! Throws: If comp throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + //! + //! Note: Iterators and references to *this are not invalidated. + template + void merge(BOOST_RV_REF(list) x, StrictWeakOrdering comp) + { this->merge(static_cast(x), comp); } + + //! Effects: This function sorts the list *this according to std::less. + //! The sort is stable, that is, the relative order of equivalent elements is preserved. + //! + //! Throws: If comparison throws. + //! + //! Notes: Iterators and references are not invalidated. + //! + //! Complexity: The number of comparisons is approximately N log N, where N + //! is the list's size. + void sort() + { this->sort(value_less()); } + + //! Effects: This function sorts the list *this according to std::less. + //! The sort is stable, that is, the relative order of equivalent elements is preserved. + //! + //! Throws: If comp throws. + //! + //! Notes: Iterators and references are not invalidated. + //! + //! Complexity: The number of comparisons is approximately N log N, where N + //! is the list's size. + template + void sort(StrictWeakOrdering comp) + { + // nothing if the list has length 0 or 1. + if (this->size() < 2) + return; + this->icont().sort(ValueCompareToNodeCompare(comp)); + } + + //! Effects: Reverses the order of elements in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: This function is linear time. + //! + //! Note: Iterators and references are not invalidated + void reverse() BOOST_CONTAINER_NOEXCEPT + { this->icont().reverse(); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const list& x, const list& y) + { + if(x.size() != y.size()){ + return false; + } + typedef typename list::const_iterator const_iterator; + const_iterator end1 = x.end(); + + const_iterator i1 = x.begin(); + const_iterator i2 = y.begin(); + while (i1 != end1 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1; + } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const list& x, const list& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const list& x, const list& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const list& x, const list& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const list& x, const list& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const list& x, const list& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(list& x, list& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + + bool priv_try_shrink(size_type new_size) + { + const size_type len = this->size(); + if(len > new_size){ + const const_iterator iend = this->cend(); + size_type to_erase = len - new_size; + const_iterator ifirst; + if(to_erase < len/2u){ + ifirst = iend; + while(to_erase--){ + --ifirst; + } + } + else{ + ifirst = this->cbegin(); + size_type to_skip = len - to_erase; + while(to_skip--){ + ++ifirst; + } + } + this->erase(ifirst, iend); + return true; + } + else{ + return false; + } + } + + iterator priv_insert(const_iterator p, const T &x) + { + NodePtr tmp = AllocHolder::create_node(x); + return iterator(this->icont().insert(p.get(), *tmp)); + } + + iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x) + { + NodePtr tmp = AllocHolder::create_node(boost::move(x)); + return iterator(this->icont().insert(p.get(), *tmp)); + } + + void priv_push_back (const T &x) + { this->insert(this->cend(), x); } + + void priv_push_back (BOOST_RV_REF(T) x) + { this->insert(this->cend(), boost::move(x)); } + + void priv_push_front (const T &x) + { this->insert(this->cbegin(), x); } + + void priv_push_front (BOOST_RV_REF(T) x) + { this->insert(this->cbegin(), boost::move(x)); } + + class insertion_functor; + friend class insertion_functor; + + class insertion_functor + { + Icont &icont_; + typedef typename Icont::const_iterator iconst_iterator; + const iconst_iterator pos_; + + public: + insertion_functor(Icont &icont, typename Icont::const_iterator pos) + : icont_(icont), pos_(pos) + {} + + void operator()(Node &n) + { + this->icont_.insert(pos_, n); + } + }; + + //Functors for member algorithm defaults + struct value_less + { + bool operator()(const value_type &a, const value_type &b) const + { return a < b; } + }; + + struct value_equal + { + bool operator()(const value_type &a, const value_type &b) const + { return a == b; } + }; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public ::boost::has_trivial_destructor_after_move +{}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} + +#include + +#endif // BOOST_CONTAINER_LIST_HPP diff --git a/boost/container/map.hpp b/boost/container/map.hpp new file mode 100644 index 0000000..6abfa1a --- /dev/null +++ b/boost/container/map.hpp @@ -0,0 +1,1406 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_MAP_HPP +#define BOOST_CONTAINER_MAP_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +namespace boost { +namespace container { + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A map is a kind of associative container that supports unique keys (contains at +//! most one of each key value) and provides for fast retrieval of values of another +//! type T based on the keys. The map class supports bidirectional iterators. +//! +//! A map satisfies all of the requirements of a container and of a reversible +//! container and of an associative container. The value_type stored +//! by this container is the value_type is std::pair. +//! +//! \tparam Key is the key_type of the map +//! \tparam Value is the mapped_type +//! \tparam Compare is the ordering function for Keys (e.g. std::less). +//! \tparam Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair > ). +//! \tparam MapOptions is an packed option type generated using using boost::container::tree_assoc_options. +template < class Key, class T, class Compare = std::less + , class Allocator = std::allocator< std::pair< const Key, T> >, class MapOptions = tree_assoc_defaults > +#else +template +#endif +class map + ///@cond + : public container_detail::tree + < Key, std::pair + , container_detail::select1st< std::pair > + , Compare, Allocator, MapOptions> + ///@endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(map) + + typedef std::pair value_type_impl; + typedef container_detail::tree + , Compare, Allocator, MapOptions> base_t; + typedef container_detail::pair movable_value_type_impl; + typedef container_detail::tree_value_compare + < Key, value_type_impl, Compare, container_detail::select1st + > value_compare_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef Key key_type; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef T mapped_type; + typedef std::pair value_type; + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::const_pointer const_pointer; + typedef typename boost::container::allocator_traits::reference reference; + typedef typename boost::container::allocator_traits::const_reference const_reference; + typedef typename boost::container::allocator_traits::size_type size_type; + typedef typename boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare; + typedef Compare key_compare; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + typedef std::pair nonconst_value_type; + typedef BOOST_CONTAINER_IMPDEF(movable_value_type_impl) movable_value_type; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty map. + //! + //! Complexity: Constant. + map() + : base_t() + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty map using the specified comparison object + //! and allocator. + //! + //! Complexity: Constant. + explicit map(const Compare& comp, + const allocator_type& a = allocator_type()) + : base_t(comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty map using the specified allocator. + //! + //! Complexity: Constant. + explicit map(const allocator_type& a) + : base_t(a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty map using the specified comparison object and + //! allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + map(InputIterator first, InputIterator last, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(true, first, last, comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty map using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + map( ordered_unique_range_t, InputIterator first, InputIterator last + , const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty map using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is il.first() - il.end(). + map(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(true, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + map(ordered_unique_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + + //! Effects: Copy constructs a map. + //! + //! Complexity: Linear in x.size(). + map(const map& x) + : base_t(static_cast(x)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a map. Constructs *this using x's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: x is emptied. + map(BOOST_RV_REF(map) x) + : base_t(boost::move(static_cast(x))) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Copy constructs a map using the specified allocator. + //! + //! Complexity: Linear in x.size(). + map(const map& x, const allocator_type &a) + : base_t(static_cast(x), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a map using the specified allocator. + //! Constructs *this using x's resources. + //! + //! Complexity: Constant if x == x.get_allocator(), linear otherwise. + //! + //! Postcondition: x is emptied. + map(BOOST_RV_REF(map) x, const allocator_type &a) + : base_t(boost::move(static_cast(x)), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + map& operator=(BOOST_COPY_ASSIGN_REF(map) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! Effects: this->swap(x.get()). + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + map& operator=(BOOST_RV_REF(map) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { return static_cast(this->base_t::operator=(boost::move(static_cast(x)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign content of il to *this. + //! + map& operator=(std::initializer_list il) + { + this->clear(); + insert(il.begin(), il.end()); + return *this; + } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Returns a copy of the Allocator that + //! was passed to the object's constructor. + //! + //! Complexity: Constant. + allocator_type get_allocator() const; + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns an iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns an iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns true if the container contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the number of the elements contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the largest possible size of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: If there is no key equivalent to x in the map, inserts + //! value_type(x, T()) into the map. + //! + //! Returns: Allocator reference to the mapped_type corresponding to x in *this. + //! + //! Complexity: Logarithmic. + mapped_type& operator[](const key_type &k); + + //! Effects: If there is no key equivalent to x in the map, inserts + //! value_type(boost::move(x), T()) into the map (the key is move-constructed) + //! + //! Returns: Allocator reference to the mapped_type corresponding to x in *this. + //! + //! Complexity: Logarithmic. + mapped_type& operator[](key_type &&k); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH( operator[] , key_type, mapped_type&, this->priv_subscript) + #endif + + //! Returns: Allocator reference to the element whose key is equivalent to x. + //! Throws: An exception object of type out_of_range if no such element is present. + //! Complexity: logarithmic. + T& at(const key_type& k) + { + iterator i = this->find(k); + if(i == this->end()){ + throw_out_of_range("map::at key not found"); + } + return i->second; + } + + //! Returns: Allocator reference to the element whose key is equivalent to x. + //! Throws: An exception object of type out_of_range if no such element is present. + //! Complexity: logarithmic. + const T& at(const key_type& k) const + { + const_iterator i = this->find(k); + if(i == this->end()){ + throw_out_of_range("map::at key not found"); + } + return i->second; + } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + //! Effects: Inserts x if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(const value_type& x) + { return this->base_t::insert_unique(x); } + + //! Effects: Inserts a new value_type created from the pair if and only if + //! there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(const nonconst_value_type& x) + { return this->base_t::insert_unique(x); } + + //! Effects: Inserts a new value_type move constructed from the pair if and + //! only if there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(BOOST_RV_REF(nonconst_value_type) x) + { return this->base_t::insert_unique(boost::move(x)); } + + //! Effects: Inserts a new value_type move constructed from the pair if and + //! only if there is no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(BOOST_RV_REF(movable_value_type) x) + { return this->base_t::insert_unique(boost::move(x)); } + + //! Effects: Move constructs a new value from x if and only if there is + //! no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(BOOST_RV_REF(value_type) x) + { return this->base_t::insert_unique(boost::move(x)); } + + //! Effects: Inserts a copy of x in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, const value_type& x) + { return this->base_t::insert_unique(p, x); } + + //! Effects: Move constructs a new value from x if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, BOOST_RV_REF(nonconst_value_type) x) + { return this->base_t::insert_unique(p, boost::move(x)); } + + //! Effects: Move constructs a new value from x if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, BOOST_RV_REF(movable_value_type) x) + { return this->base_t::insert_unique(p, boost::move(x)); } + + //! Effects: Inserts a copy of x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + iterator insert(const_iterator p, const nonconst_value_type& x) + { return this->base_t::insert_unique(p, x); } + + //! Effects: Inserts an element move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + iterator insert(const_iterator p, BOOST_RV_REF(value_type) x) + { return this->base_t::insert_unique(p, boost::move(x)); } + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_unique(first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + void insert(std::initializer_list il) + { this->base_t::insert_unique(il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object x of type T constructed with + //! std::forward(args)... in the container if and only if there is + //! no element in the container with an equivalent key. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + template + std::pair emplace(Args&&... args) + { return this->base_t::emplace_unique(boost::forward(args)...); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the container if and only if there is + //! no element in the container with an equivalent key. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_unique(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); }\ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_unique(p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _));} \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Erases the element pointed to by p. + //! + //! Returns: Returns an iterator pointing to the element immediately + //! following q prior to the element being erased. If no such element exists, + //! returns end(). + //! + //! Complexity: Amortized constant time + iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Erases all elements in the container with key equivalent to x. + //! + //! Returns: Returns the number of erased elements. + //! + //! Complexity: log(size()) + count(k) + size_type erase(const key_type& x) BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Erases all the elements in the range [first, last). + //! + //! Returns: Returns last. + //! + //! Complexity: log(size())+N where N is the distance from first to last. + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(map& x); + + //! Effects: erase(a.begin(),a.end()). + //! + //! Postcondition: size() == 0. + //! + //! Complexity: linear in size(). + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! Effects: Returns the comparison object out + //! of which a was constructed. + //! + //! Complexity: Constant. + key_compare key_comp() const; + + //! Effects: Returns an object of value_compare constructed out + //! of the comparison object. + //! + //! Complexity: Constant. + value_compare value_comp() const; + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x); + + //! Returns: Allocator const_iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + const_iterator find(const key_type& x) const; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const + { return static_cast(this->find(x) != this->cend()); } + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const; + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const; + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x); + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const; + + //! Effects: Rebalances the tree. It's a no-op for Red-Black and AVL trees. + //! + //! Complexity: Linear + void rebalance(); + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const map& x, const map& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const map& x, const map& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const map& x, const map& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const map& x, const map& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const map& x, const map& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const map& x, const map& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(map& x, map& y); + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + mapped_type& priv_subscript(const key_type &k) + { + //we can optimize this + iterator i = this->lower_bound(k); + // i->first is greater than or equivalent to k. + if (i == this->end() || this->key_comp()(k, (*i).first)){ + container_detail::value_init m; + movable_value_type val(k, boost::move(m.m_t)); + i = insert(i, boost::move(val)); + } + return (*i).second; + } + + mapped_type& priv_subscript(BOOST_RV_REF(key_type) mk) + { + key_type &k = mk; + //we can optimize this + iterator i = this->lower_bound(k); + // i->first is greater than or equivalent to k. + if (i == this->end() || this->key_comp()(k, (*i).first)){ + container_detail::value_init m; + movable_value_type val(boost::move(k), boost::move(m.m_t)); + i = insert(i, boost::move(val)); + } + return (*i).second; + } + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A multimap is a kind of associative container that supports equivalent keys +//! (possibly containing multiple copies of the same key value) and provides for +//! fast retrieval of values of another type T based on the keys. The multimap class +//! supports bidirectional iterators. +//! +//! A multimap satisfies all of the requirements of a container and of a reversible +//! container and of an associative container. The value_type stored +//! by this container is the value_type is std::pair. +//! +//! \tparam Key is the key_type of the map +//! \tparam Value is the mapped_type +//! \tparam Compare is the ordering function for Keys (e.g. std::less). +//! \tparam Allocator is the allocator to allocate the value_types +//! (e.g. allocator< std::pair > ). +//! \tparam MultiMapOptions is an packed option type generated using using boost::container::tree_assoc_options. +template < class Key, class T, class Compare = std::less + , class Allocator = std::allocator< std::pair< const Key, T> >, class MultiMapOptions = tree_assoc_defaults> +#else +template +#endif +class multimap + ///@cond + : public container_detail::tree + < Key, std::pair + , container_detail::select1st< std::pair > + , Compare, Allocator, MultiMapOptions> + ///@endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(multimap) + + typedef std::pair value_type_impl; + typedef container_detail::tree + , Compare, Allocator, MultiMapOptions> base_t; + typedef container_detail::pair movable_value_type_impl; + typedef container_detail::tree_value_compare + < Key, value_type_impl, Compare, container_detail::select1st + > value_compare_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef Key key_type; + typedef T mapped_type; + typedef std::pair value_type; + typedef typename boost::container::allocator_traits::pointer pointer; + typedef typename boost::container::allocator_traits::const_pointer const_pointer; + typedef typename boost::container::allocator_traits::reference reference; + typedef typename boost::container::allocator_traits::const_reference const_reference; + typedef typename boost::container::allocator_traits::size_type size_type; + typedef typename boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(value_compare_impl) value_compare; + typedef Compare key_compare; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + typedef std::pair nonconst_value_type; + typedef BOOST_CONTAINER_IMPDEF(movable_value_type_impl) movable_value_type; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty multimap. + //! + //! Complexity: Constant. + multimap() + : base_t() + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty multimap using the specified allocator. + //! + //! Complexity: Constant. + explicit multimap(const Compare& comp, const allocator_type& a = allocator_type()) + : base_t(comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty multimap using the specified comparison + //! object and allocator. + //! + //! Complexity: Constant. + explicit multimap(const allocator_type& a) + : base_t(a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty multimap using the specified comparison object + //! and allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + multimap(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(false, first, last, comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty multimap using the specified comparison object and + //! allocator, and inserts elements from the ordered range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + multimap(ordered_range_t, InputIterator first, InputIterator last, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty multimap using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is il.first() - il.end(). + multimap(std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(false, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + multimap(ordered_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + + //! Effects: Copy constructs a multimap. + //! + //! Complexity: Linear in x.size(). + multimap(const multimap& x) + : base_t(static_cast(x)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a multimap. Constructs *this using x's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: x is emptied. + multimap(BOOST_RV_REF(multimap) x) + : base_t(boost::move(static_cast(x))) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Copy constructs a multimap. + //! + //! Complexity: Linear in x.size(). + multimap(const multimap& x, const allocator_type &a) + : base_t(static_cast(x), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Move constructs a multimap using the specified allocator. + //! Constructs *this using x's resources. + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + //! + //! Postcondition: x is emptied. + multimap(BOOST_RV_REF(multimap) x, const allocator_type &a) + : base_t(boost::move(static_cast(x)), a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + multimap& operator=(BOOST_COPY_ASSIGN_REF(multimap) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! Effects: this->swap(x.get()). + //! + //! Complexity: Constant. + multimap& operator=(BOOST_RV_REF(multimap) x) + { return static_cast(this->base_t::operator=(boost::move(static_cast(x)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign content of il to *this. + //! + multimap& operator=(std::initializer_list il) + { + this->clear(); + insert(il.begin(), il.end()); + return *this; + } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::set::get_allocator() + allocator_type get_allocator() const; + + //! @copydoc ::boost::container::set::get_stored_allocator() + stored_allocator_type &get_stored_allocator(); + + //! @copydoc ::boost::container::set::get_stored_allocator() const + const stored_allocator_type &get_stored_allocator() const; + + //! @copydoc ::boost::container::set::begin() + iterator begin(); + + //! @copydoc ::boost::container::set::begin() const + const_iterator begin() const; + + //! @copydoc ::boost::container::set::cbegin() const + const_iterator cbegin() const; + + //! @copydoc ::boost::container::set::end() + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::end() const + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::cend() const + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rbegin() + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rbegin() const + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::crbegin() const + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rend() + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rend() const + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::crend() const + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::empty() const + bool empty() const; + + //! @copydoc ::boost::container::set::size() const + size_type size() const; + + //! @copydoc ::boost::container::set::max_size() const + size_type max_size() const; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + template + iterator emplace(Args&&... args) + { return this->base_t::emplace_equal(boost::forward(args)...); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_equal(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_equal(p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _));} \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + //! Effects: Inserts x and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic. + iterator insert(const value_type& x) + { return this->base_t::insert_equal(x); } + + //! Effects: Inserts a new value constructed from x and returns + //! the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic. + iterator insert(const nonconst_value_type& x) + { return this->base_t::insert_equal(x); } + + //! Effects: Inserts a new value move-constructed from x and returns + //! the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic. + iterator insert(BOOST_RV_REF(nonconst_value_type) x) + { return this->base_t::insert_equal(boost::move(x)); } + + //! Effects: Inserts a new value move-constructed from x and returns + //! the iterator pointing to the newly inserted element. + //! + //! Complexity: Logarithmic. + iterator insert(BOOST_RV_REF(movable_value_type) x) + { return this->base_t::insert_equal(boost::move(x)); } + + //! Effects: Inserts a copy of x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, const value_type& x) + { return this->base_t::insert_equal(p, x); } + + //! Effects: Inserts a new value constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, const nonconst_value_type& x) + { return this->base_t::insert_equal(p, x); } + + //! Effects: Inserts a new value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, BOOST_RV_REF(nonconst_value_type) x) + { return this->base_t::insert_equal(p, boost::move(x)); } + + //! Effects: Inserts a new value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, BOOST_RV_REF(movable_value_type) x) + { return this->base_t::insert_equal(p, boost::move(x)); } + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_equal(first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end(). + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + void insert(std::initializer_list il) + { this->base_t::insert_equal(il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::set::erase(const_iterator) + iterator erase(const_iterator p); + + //! @copydoc ::boost::container::set::erase(const key_type&) + size_type erase(const key_type& x); + + //! @copydoc ::boost::container::set::erase(const_iterator,const_iterator) + iterator erase(const_iterator first, const_iterator last); + + //! @copydoc ::boost::container::set::swap + void swap(flat_multiset& x); + + //! @copydoc ::boost::container::set::clear + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::key_comp + key_compare key_comp() const; + + //! @copydoc ::boost::container::set::value_comp + value_compare value_comp() const; + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x); + + //! Returns: Allocator const iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + const_iterator find(const key_type& x) const; + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const; + + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const; + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const; + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x); + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const; + + //! Effects: Rebalances the tree. It's a no-op for Red-Black and AVL trees. + //! + //! Complexity: Linear + void rebalance(); + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const multimap& x, const multimap& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const multimap& x, const multimap& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const multimap& x, const multimap& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const multimap& x, const multimap& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const multimap& x, const multimap& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const multimap& x, const multimap& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(multimap& x, multimap& y); + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} + +#include + +#endif /* BOOST_CONTAINER_MAP_HPP */ + diff --git a/boost/container/node_allocator.hpp b/boost/container/node_allocator.hpp new file mode 100644 index 0000000..8004339 --- /dev/null +++ b/boost/container/node_allocator.hpp @@ -0,0 +1,344 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_POOLED_NODE_ALLOCATOR_HPP +#define BOOST_CONTAINER_POOLED_NODE_ALLOCATOR_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +//!An STL node allocator that uses a modified DlMalloc as memory +//!source. +//! +//!This node allocator shares a segregated storage between all instances +//!of node_allocator with equal sizeof(T). +//! +//!NodesPerBlock is the number of nodes allocated at once when the allocator +//!runs out of nodes +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template + < class T + , std::size_t NodesPerBlock = NodeAlloc_nodes_per_block> +#else +template + < class T + , std::size_t NodesPerBlock + , std::size_t Version> +#endif +class node_allocator +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + //! If Version is 1, the allocator is a STL conforming allocator. If Version is 2, + //! the allocator offers advanced expand in place and burst allocation capabilities. + public: + typedef unsigned int allocation_type; + typedef node_allocator self_t; + + static const std::size_t nodes_per_block = NodesPerBlock; + + BOOST_STATIC_ASSERT((Version <=2)); + #endif + + public: + //------- + typedef T value_type; + typedef T * pointer; + typedef const T * const_pointer; + typedef typename ::boost::container:: + container_detail::unvoid::type & reference; + typedef const typename ::boost::container:: + container_detail::unvoid::type & const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + typedef boost::container::container_detail:: + version_type version; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef boost::container::container_detail:: + basic_multiallocation_chain multiallocation_chain_void; + typedef boost::container::container_detail:: + transform_multiallocation_chain + multiallocation_chain; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //!Obtains node_allocator from + //!node_allocator + template + struct rebind + { + typedef node_allocator< T2, NodesPerBlock + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + , Version + #endif + > other; + }; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + //!Not assignable from related node_allocator + template + node_allocator& operator= + (const node_allocator&); + + //!Not assignable from other node_allocator + node_allocator& operator=(const node_allocator&); + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + //!Default constructor + node_allocator() BOOST_CONTAINER_NOEXCEPT + {} + + //!Copy constructor from other node_allocator. + node_allocator(const node_allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Copy constructor from related node_allocator. + template + node_allocator + (const node_allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + //!Destructor + ~node_allocator() BOOST_CONTAINER_NOEXCEPT + {} + + //!Returns the number of elements that could be allocated. + //!Never throws + size_type max_size() const + { return size_type(-1)/sizeof(T); } + + //!Allocate memory for an array of count elements. + //!Throws std::bad_alloc if there is no enough memory + pointer allocate(size_type count, const void * = 0) + { + if(count > this->max_size()) + boost::container::throw_bad_alloc(); + + if(Version == 1 && count == 1){ + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + return pointer(static_cast(singleton_t::instance().allocate_node())); + } + else{ + void *ret = boost_cont_malloc(count*sizeof(T)); + if(!ret) + boost::container::throw_bad_alloc(); + return static_cast(ret); + } + } + + //!Deallocate allocated memory. + //!Never throws + void deallocate(const pointer &ptr, size_type count) BOOST_CONTAINER_NOEXCEPT + { + (void)count; + if(Version == 1 && count == 1){ + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_node(ptr); + } + else{ + boost_cont_free(ptr); + } + } + + //!Deallocates all free blocks of the pool + static void deallocate_free_blocks() BOOST_CONTAINER_NOEXCEPT + { + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_free_blocks(); + } + + std::pair + allocation_command(allocation_type command, + size_type limit_size, + size_type preferred_size, + size_type &received_size, pointer reuse = pointer()) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + std::pair ret = + priv_allocation_command(command, limit_size, preferred_size, received_size, reuse); + if(!ret.first && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)) + boost::container::throw_bad_alloc(); + return ret; + } + + //!Returns maximum the number of objects the previously allocated memory + //!pointed by p can hold. + size_type size(pointer p) const BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + return boost_cont_size(p); + } + + //!Allocates just one object. Memory allocated with this function + //!must be deallocated only with deallocate_one(). + //!Throws bad_alloc if there is no enough memory + pointer allocate_one() + { + BOOST_STATIC_ASSERT(( Version > 1 )); + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + return (pointer)singleton_t::instance().allocate_node(); + } + + //!Allocates many elements of size == 1. + //!Elements must be individually deallocated with deallocate_one() + void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + typename shared_pool_t::multiallocation_chain ch; + singleton_t::instance().allocate_nodes(num_elements, ch); + chain.incorporate_after(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size()); + } + + //!Deallocates memory previously allocated with allocate_one(). + //!You should never use deallocate_one to deallocate memory allocated + //!with other functions different from allocate_one(). Never throws + void deallocate_one(pointer p) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + singleton_t::instance().deallocate_node(p); + } + + void deallocate_individual(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + typedef container_detail::shared_node_pool + shared_pool_t; + typedef container_detail::singleton_default singleton_t; + typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size()); + singleton_t::instance().deallocate_nodes(ch); + } + + //!Allocates many elements of size elem_size. + //!Elements must be individually deallocated with deallocate() + void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT(&ch); + if(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after( chain.before_begin() + , (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + , (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + , BOOST_CONTAINER_MEMCHAIN_SIZE(&ch)); + } + + //!Allocates n_elements elements, each one of size elem_sizes[i] + //!Elements must be individually deallocated with deallocate() + void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) + { + BOOST_STATIC_ASSERT(( Version > 1 )); + boost_cont_memchain ch; + boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch); + if(BOOST_CONTAINER_MEMCHAIN_EMPTY(&ch)){ + boost::container::throw_bad_alloc(); + } + chain.incorporate_after( chain.before_begin() + , (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + , (T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) + , BOOST_CONTAINER_MEMCHAIN_SIZE(&ch)); + } + + void deallocate_many(multiallocation_chain &chain) BOOST_CONTAINER_NOEXCEPT + { + BOOST_STATIC_ASSERT(( Version > 1 )); + void *first = &*chain.begin(); + void *last = &*chain.last(); + size_t num = chain.size(); + boost_cont_memchain ch; + BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, first, last, num); + boost_cont_multidealloc(&ch); + } + + //!Swaps allocators. Does not throw. If each allocator is placed in a + //!different memory segment, the result is undefined. + friend void swap(self_t &, self_t &) BOOST_CONTAINER_NOEXCEPT + {} + + //!An allocator always compares to true, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator==(const node_allocator &, const node_allocator &) BOOST_CONTAINER_NOEXCEPT + { return true; } + + //!An allocator always compares to false, as memory allocated with one + //!instance can be deallocated by another instance + friend bool operator!=(const node_allocator &, const node_allocator &) BOOST_CONTAINER_NOEXCEPT + { return false; } + + private: + std::pair priv_allocation_command + (allocation_type command, std::size_t limit_size + ,std::size_t preferred_size,std::size_t &received_size, void *reuse_ptr) + { + boost_cont_command_ret_t ret = {0 , 0}; + if(limit_size > this->max_size() || preferred_size > this->max_size()){ + //ret.first = 0; + return std::pair(pointer(), false); + } + std::size_t l_size = limit_size*sizeof(T); + std::size_t p_size = preferred_size*sizeof(T); + std::size_t r_size; + { + ret = boost_cont_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr); + } + received_size = r_size/sizeof(T); + return std::pair(static_cast(ret.first), !!ret.second); + } +}; + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_POOLED_NODE_ALLOCATOR_HPP diff --git a/boost/container/options.hpp b/boost/container/options.hpp new file mode 100644 index 0000000..c36ad30 --- /dev/null +++ b/boost/container/options.hpp @@ -0,0 +1,76 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2013-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_OPTIONS_HPP +#define BOOST_CONTAINER_OPTIONS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace container { + +#if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + +template +struct tree_opt +{ + static const boost::container::tree_type_enum tree_type = TreeType; + static const bool optimize_size = OptimizeSize; +}; + +#endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + +//!This option setter specifies the underlying tree type +//!(red-black, AVL, Scapegoat or Splay) for ordered associative containers +BOOST_INTRUSIVE_OPTION_CONSTANT(tree_type, tree_type_enum, TreeType, tree_type) + +//!This option setter specifies if node size is optimized +//!storing rebalancing data masked into pointers for ordered associative containers +BOOST_INTRUSIVE_OPTION_CONSTANT(optimize_size, bool, Enabled, optimize_size) + +//! Helper metafunction to combine options into a single type to be used +//! by \c boost::container::set, \c boost::container::multiset +//! \c boost::container::map and \c boost::container::multimap. +//! Supported options are: \c boost::container::optimize_size and \c boost::container::tree_type +#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) || defined(BOOST_CONTAINER_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct tree_assoc_options +{ + /// @cond + typedef typename ::boost::intrusive::pack_options + < tree_assoc_defaults, + #if !defined(BOOST_CONTAINER_VARIADIC_TEMPLATES) + O1, O2, O3, O4 + #else + Options... + #endif + >::type packed_options; + typedef tree_opt implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +} //namespace container { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_CONTAINER_OPTIONS_HPP diff --git a/boost/container/scoped_allocator.hpp b/boost/container/scoped_allocator.hpp new file mode 100644 index 0000000..0a29871 --- /dev/null +++ b/boost/container/scoped_allocator.hpp @@ -0,0 +1,1534 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Pablo Halpern 2009. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_HPP +#define BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_HPP + +#if defined (_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { namespace container { + +//! Remark: if a specialization is derived from true_type, indicates that T may be constructed +//! with an allocator as its last constructor argument. Ideally, all constructors of T (including the +//! copy and move constructors) should have a variant that accepts a final argument of +//! allocator_type. +//! +//! Requires: if a specialization is derived from true_type, T must have a nested type, +//! allocator_type and at least one constructor for which allocator_type is the last +//! parameter. If not all constructors of T can be called with a final allocator_type argument, +//! and if T is used in a context where a container must call such a constructor, then the program is +//! ill-formed. +//! +//! +//! template > +//! class Z { +//! public: +//! typedef Allocator allocator_type; +//! +//! // Default constructor with optional allocator suffix +//! Z(const allocator_type& a = allocator_type()); +//! +//! // Copy constructor and allocator-extended copy constructor +//! Z(const Z& zz); +//! Z(const Z& zz, const allocator_type& a); +//! }; +//! +//! // Specialize trait for class template Z +//! template > +//! struct constructible_with_allocator_suffix > +//! : ::boost::true_type { }; +//! +//! +//! Note: This trait is a workaround inspired by "N2554: The Scoped Allocator Model (Rev 2)" +//! (Pablo Halpern, 2008-02-29) to backport the scoped allocator model to C++03, as +//! in C++03 there is no mechanism to detect if a type can be constructed from arbitrary arguments. +//! Applications aiming portability with several compilers should always define this trait. +//! +//! In conforming C++11 compilers or compilers supporting SFINAE expressions +//! (when BOOST_NO_SFINAE_EXPR is NOT defined), this trait is ignored and C++11 rules will be used +//! to detect if a type should be constructed with suffix or prefix allocator arguments. +template +struct constructible_with_allocator_suffix + : ::boost::false_type +{}; + +//! Remark: if a specialization is derived from true_type, indicates that T may be constructed +//! with allocator_arg and T::allocator_type as its first two constructor arguments. +//! Ideally, all constructors of T (including the copy and move constructors) should have a variant +//! that accepts these two initial arguments. +//! +//! Requires: if a specialization is derived from true_type, T must have a nested type, +//! allocator_type and at least one constructor for which allocator_arg_t is the first +//! parameter and allocator_type is the second parameter. If not all constructors of T can be +//! called with these initial arguments, and if T is used in a context where a container must call such +//! a constructor, then the program is ill-formed. +//! +//! +//! template > +//! class Y { +//! public: +//! typedef Allocator allocator_type; +//! +//! // Default constructor with and allocator-extended default constructor +//! Y(); +//! Y(allocator_arg_t, const allocator_type& a); +//! +//! // Copy constructor and allocator-extended copy constructor +//! Y(const Y& yy); +//! Y(allocator_arg_t, const allocator_type& a, const Y& yy); +//! +//! // Variadic constructor and allocator-extended variadic constructor +//! template Y(Args&& args...); +//! template +//! Y(allocator_arg_t, const allocator_type& a, Args&&... args); +//! }; +//! +//! // Specialize trait for class template Y +//! template > +//! struct constructible_with_allocator_prefix > +//! : ::boost::true_type { }; +//! +//! +//! +//! Note: This trait is a workaround inspired by "N2554: The Scoped Allocator Model (Rev 2)" +//! (Pablo Halpern, 2008-02-29) to backport the scoped allocator model to C++03, as +//! in C++03 there is no mechanism to detect if a type can be constructed from arbitrary arguments. +//! Applications aiming portability with several compilers should always define this trait. +//! +//! In conforming C++11 compilers or compilers supporting SFINAE expressions +//! (when BOOST_NO_SFINAE_EXPR is NOT defined), this trait is ignored and C++11 rules will be used +//! to detect if a type should be constructed with suffix or prefix allocator arguments. +template +struct constructible_with_allocator_prefix + : ::boost::false_type +{}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace container_detail { + +template +struct uses_allocator_imp +{ + // Use SFINAE (Substitution Failure Is Not An Error) to detect the + // presence of an 'allocator_type' nested type convertilble from Alloc. + + private: + // Match this function if TypeT::allocator_type exists and is + // implicitly convertible from Alloc + template + static char test(int, typename U::allocator_type); + + // Match this function if TypeT::allocator_type does not exist or is + // not convertible from Alloc. + template + static int test(LowPriorityConversion, LowPriorityConversion); + + static Alloc alloc; // Declared but not defined + + public: + enum { value = sizeof(test(0, alloc)) == sizeof(char) }; +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! Remark: Automatically detects if T has a nested allocator_type that is convertible from +//! Alloc. Meets the BinaryTypeTrait requirements ([meta.rqmts] 20.4.1). A program may +//! specialize this type to derive from true_type for a T of user-defined type if T does not +//! have a nested allocator_type but is nonetheless constructible using the specified Alloc. +//! +//! Result: derived from true_type if Convertible and +//! derived from false_type otherwise. +template +struct uses_allocator + : boost::integral_constant::value> +{}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace container_detail { + +template +struct is_scoped_allocator_imp +{ + template + static char test(int, typename T::outer_allocator_type*); + + template + static int test(LowPriorityConversion, void*); + + static const bool value = (sizeof(char) == sizeof(test(0, 0))); +}; + +template::value > +struct outermost_allocator_type_impl +{ + typedef typename MaybeScopedAlloc::outer_allocator_type outer_type; + typedef typename outermost_allocator_type_impl::type type; +}; + +template +struct outermost_allocator_type_impl +{ + typedef MaybeScopedAlloc type; +}; + +template::value > +struct outermost_allocator_imp +{ + typedef MaybeScopedAlloc type; + + static type &get(MaybeScopedAlloc &a) + { return a; } + + static const type &get(const MaybeScopedAlloc &a) + { return a; } +}; + +template +struct outermost_allocator_imp +{ + typedef typename MaybeScopedAlloc::outer_allocator_type outer_type; + typedef typename outermost_allocator_type_impl::type type; + + static type &get(MaybeScopedAlloc &a) + { return outermost_allocator_imp::get(a.outer_allocator()); } + + static const type &get(const MaybeScopedAlloc &a) + { return outermost_allocator_imp::get(a.outer_allocator()); } +}; + +} //namespace container_detail { + +template +struct is_scoped_allocator + : boost::integral_constant::value> +{}; + +template +struct outermost_allocator + : container_detail::outermost_allocator_imp +{}; + +template +typename container_detail::outermost_allocator_imp::type & + get_outermost_allocator(Alloc &a) +{ return container_detail::outermost_allocator_imp::get(a); } + +template +const typename container_detail::outermost_allocator_imp::type & + get_outermost_allocator(const Alloc &a) +{ return container_detail::outermost_allocator_imp::get(a); } + +namespace container_detail { + +// Check if we can detect is_convertible using advanced SFINAE expressions +#if !defined(BOOST_NO_SFINAE_EXPR) + + //! Code inspired by Mathias Gaunard's is_convertible.cpp found in the Boost mailing list + //! http://boost.2283326.n4.nabble.com/type-traits-is-constructible-when-decltype-is-supported-td3575452.html + //! Thanks Mathias! + + //With variadic templates, we need a single class to implement the trait + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template + struct is_constructible_impl + { + typedef char yes_type; + struct no_type + { char padding[2]; }; + + template + struct dummy; + + template + static yes_type test(dummy()...))>*); + + template + static no_type test(...); + + static const bool value = sizeof(test(0)) == sizeof(yes_type); + }; + + template + struct is_constructible + : boost::integral_constant::value> + {}; + + template + struct is_constructible_with_allocator_prefix + : is_constructible + {}; + + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + //Without variadic templates, we need to use the preprocessor to generate + //some specializations. + + #define BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS \ + BOOST_PP_ADD(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, 3) + //! + + //Generate N+1 template parameters so that we can specialize N + template + struct is_constructible_impl; + + //Generate N specializations, from 0 to + //BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS parameters + #define BOOST_PP_LOCAL_MACRO(n) \ + template \ + struct is_constructible_impl \ + \ + { \ + typedef char yes_type; \ + struct no_type \ + { char padding[2]; }; \ + \ + template \ + struct dummy; \ + \ + template \ + static yes_type test(dummy*); \ + \ + template \ + static no_type test(...); \ + \ + static const bool value = sizeof(test(0)) == sizeof(yes_type); \ + }; \ + //! + + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + //Finally just inherit from the implementation to define he trait + template< class T + BOOST_PP_ENUM_TRAILING( BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS + , BOOST_CONTAINER_PP_TEMPLATE_PARAM_WITH_DEFAULT + , void) + > + struct is_constructible + : boost::integral_constant + < bool + , is_constructible_impl + < T + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS, P) + , void>::value + > + {}; + + //Finally just inherit from the implementation to define he trait + template + struct is_constructible_with_allocator_prefix + : is_constructible + < T, allocator_arg_t, InnerAlloc + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS, 2), P) + > + {}; +/* + template + struct is_constructible_with_allocator_suffix + : is_constructible + < T + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_CONTAINER_MAX_IS_CONSTRUCTIBLE_PARAMETERS, 1), P) + , InnerAlloc + > + {};*/ + + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +#else // #if !defined(BOOST_NO_SFINAE_EXPR) + + //Without advanced SFINAE expressions, we can't use is_constructible + //so backup to constructible_with_allocator_xxx + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template < class T, class InnerAlloc, class ...Args> + struct is_constructible_with_allocator_prefix + : constructible_with_allocator_prefix + {}; +/* + template < class T, class InnerAlloc, class ...Args> + struct is_constructible_with_allocator_suffix + : constructible_with_allocator_suffix + {};*/ + + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template < class T + , class InnerAlloc + BOOST_PP_ENUM_TRAILING( BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS + , BOOST_CONTAINER_PP_TEMPLATE_PARAM_WITH_DEFAULT + , void) + > + struct is_constructible_with_allocator_prefix + : constructible_with_allocator_prefix + {}; +/* + template < class T + , class InnerAlloc + BOOST_PP_ENUM_TRAILING( BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS + , BOOST_CONTAINER_PP_TEMPLATE_PARAM_WITH_DEFAULT + , void) + > + struct is_constructible_with_allocator_suffix + : constructible_with_allocator_suffix + {};*/ + + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +#endif // #if !defined(BOOST_NO_SFINAE_EXPR) + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +template < typename OutermostAlloc + , typename InnerAlloc + , typename T + , class ...Args + > +inline void dispatch_allocator_prefix_suffix + ( boost::true_type use_alloc_prefix, OutermostAlloc& outermost_alloc + , InnerAlloc& inner_alloc, T* p, BOOST_FWD_REF(Args) ...args) +{ + (void)use_alloc_prefix; + allocator_traits::construct + ( outermost_alloc, p, allocator_arg, inner_alloc, ::boost::forward(args)...); +} + +template < typename OutermostAlloc + , typename InnerAlloc + , typename T + , class ...Args + > +inline void dispatch_allocator_prefix_suffix + ( boost::false_type use_alloc_prefix, OutermostAlloc& outermost_alloc + , InnerAlloc &inner_alloc, T* p, BOOST_FWD_REF(Args)...args) +{ + (void)use_alloc_prefix; + allocator_traits::construct + (outermost_alloc, p, ::boost::forward(args)..., inner_alloc); +} + +template < typename OutermostAlloc + , typename InnerAlloc + , typename T + , class ...Args + > +inline void dispatch_uses_allocator + ( boost::true_type uses_allocator, OutermostAlloc& outermost_alloc + , InnerAlloc& inner_alloc, T* p, BOOST_FWD_REF(Args)...args) +{ + (void)uses_allocator; + //BOOST_STATIC_ASSERT((is_constructible_with_allocator_prefix::value || + // is_constructible_with_allocator_suffix::value )); + dispatch_allocator_prefix_suffix + ( is_constructible_with_allocator_prefix() + , outermost_alloc, inner_alloc, p, ::boost::forward(args)...); +} + +template < typename OutermostAlloc + , typename InnerAlloc + , typename T + , class ...Args + > +inline void dispatch_uses_allocator + ( boost::false_type uses_allocator, OutermostAlloc & outermost_alloc + , InnerAlloc & inner_alloc + ,T* p, BOOST_FWD_REF(Args)...args) +{ + (void)uses_allocator; (void)inner_alloc; + allocator_traits::construct + (outermost_alloc, p, ::boost::forward(args)...); +} + +#else //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +#define BOOST_PP_LOCAL_MACRO(n) \ +template < typename OutermostAlloc \ + , typename InnerAlloc \ + , typename T \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) \ + > \ +inline void dispatch_allocator_prefix_suffix( \ + boost::true_type use_alloc_prefix, \ + OutermostAlloc& outermost_alloc, \ + InnerAlloc& inner_alloc, \ + T* p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ +{ \ + (void)use_alloc_prefix, \ + allocator_traits::construct \ + (outermost_alloc, p, allocator_arg, inner_alloc \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ +} \ + \ +template < typename OutermostAlloc \ + , typename InnerAlloc \ + , typename T \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) \ + > \ +inline void dispatch_allocator_prefix_suffix( \ + boost::false_type use_alloc_prefix, \ + OutermostAlloc& outermost_alloc, \ + InnerAlloc& inner_alloc, \ + T* p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ +{ \ + (void)use_alloc_prefix; \ + allocator_traits::construct \ + (outermost_alloc, p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) \ + , inner_alloc); \ +} \ + \ +template < typename OutermostAlloc \ + , typename InnerAlloc \ + , typename T \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) \ + > \ +inline void dispatch_uses_allocator(boost::true_type uses_allocator, \ + OutermostAlloc& outermost_alloc, \ + InnerAlloc& inner_alloc, \ + T* p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ +{ \ + (void)uses_allocator; \ + dispatch_allocator_prefix_suffix \ + (is_constructible_with_allocator_prefix \ + < T, InnerAlloc BOOST_PP_ENUM_TRAILING_PARAMS(n, P)>() \ + , outermost_alloc, inner_alloc, p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ +} \ + \ +template < typename OutermostAlloc \ + , typename InnerAlloc \ + , typename T \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) \ + > \ +inline void dispatch_uses_allocator(boost::false_type uses_allocator \ + ,OutermostAlloc & outermost_alloc \ + ,InnerAlloc & inner_alloc \ + ,T* p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ +{ \ + (void)uses_allocator; (void)inner_alloc; \ + allocator_traits::construct \ + (outermost_alloc, p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ +} \ +//! +#define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) +#include BOOST_PP_LOCAL_ITERATE() + +#endif //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +template +class scoped_allocator_adaptor_base + : public OuterAlloc +{ + typedef allocator_traits outer_traits_type; + BOOST_COPYABLE_AND_MOVABLE(scoped_allocator_adaptor_base) + + public: + template + struct rebind_base + { + typedef scoped_allocator_adaptor_base other; + }; + + typedef OuterAlloc outer_allocator_type; + typedef scoped_allocator_adaptor inner_allocator_type; + typedef allocator_traits inner_traits_type; + typedef scoped_allocator_adaptor + scoped_allocator_type; + typedef boost::integral_constant< + bool, + outer_traits_type::propagate_on_container_copy_assignment::value || + inner_allocator_type::propagate_on_container_copy_assignment::value + > propagate_on_container_copy_assignment; + typedef boost::integral_constant< + bool, + outer_traits_type::propagate_on_container_move_assignment::value || + inner_allocator_type::propagate_on_container_move_assignment::value + > propagate_on_container_move_assignment; + typedef boost::integral_constant< + bool, + outer_traits_type::propagate_on_container_swap::value || + inner_allocator_type::propagate_on_container_swap::value + > propagate_on_container_swap; + + scoped_allocator_adaptor_base() + {} + + template + scoped_allocator_adaptor_base(BOOST_FWD_REF(OuterA2) outerAlloc, const InnerAllocs &...args) + : outer_allocator_type(::boost::forward(outerAlloc)) + , m_inner(args...) + {} + + scoped_allocator_adaptor_base(const scoped_allocator_adaptor_base& other) + : outer_allocator_type(other.outer_allocator()) + , m_inner(other.inner_allocator()) + {} + + scoped_allocator_adaptor_base(BOOST_RV_REF(scoped_allocator_adaptor_base) other) + : outer_allocator_type(::boost::move(other.outer_allocator())) + , m_inner(::boost::move(other.inner_allocator())) + {} + + template + scoped_allocator_adaptor_base + (const scoped_allocator_adaptor_base& other) + : outer_allocator_type(other.outer_allocator()) + , m_inner(other.inner_allocator()) + {} + + template + scoped_allocator_adaptor_base + (BOOST_RV_REF_BEG scoped_allocator_adaptor_base + BOOST_RV_REF_END other) + : outer_allocator_type(other.outer_allocator()) + , m_inner(other.inner_allocator()) + {} + + public: + struct internal_type_t{}; + + template + scoped_allocator_adaptor_base + ( internal_type_t + , BOOST_FWD_REF(OuterA2) outerAlloc + , const inner_allocator_type &inner) + : outer_allocator_type(::boost::forward(outerAlloc)) + , m_inner(inner) + {} + + public: + + scoped_allocator_adaptor_base &operator= + (BOOST_COPY_ASSIGN_REF(scoped_allocator_adaptor_base) other) + { + outer_allocator_type::operator=(other.outer_allocator()); + m_inner = other.inner_allocator(); + return *this; + } + + scoped_allocator_adaptor_base &operator=(BOOST_RV_REF(scoped_allocator_adaptor_base) other) + { + outer_allocator_type::operator=(boost::move(other.outer_allocator())); + m_inner = ::boost::move(other.inner_allocator()); + return *this; + } + + void swap(scoped_allocator_adaptor_base &r) + { + boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator()); + boost::container::swap_dispatch(this->m_inner, r.inner_allocator()); + } + + friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r) + { l.swap(r); } + + inner_allocator_type& inner_allocator() BOOST_CONTAINER_NOEXCEPT + { return m_inner; } + + inner_allocator_type const& inner_allocator() const BOOST_CONTAINER_NOEXCEPT + { return m_inner; } + + outer_allocator_type & outer_allocator() BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this); } + + const outer_allocator_type &outer_allocator() const BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this); } + + scoped_allocator_type select_on_container_copy_construction() const + { + return scoped_allocator_type + (internal_type_t() + ,outer_traits_type::select_on_container_copy_construction(this->outer_allocator()) + ,inner_traits_type::select_on_container_copy_construction(this->inner_allocator()) + ); + } + + private: + inner_allocator_type m_inner; +}; + +#else //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +//Let's add a dummy first template parameter to allow creating +//specializations up to maximum InnerAlloc count +template < + typename OuterAlloc + , bool Dummy + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, class Q) + > +class scoped_allocator_adaptor_base; + +//Specializations for the adaptor with InnerAlloc allocators + +#define BOOST_PP_LOCAL_MACRO(n) \ +template \ +class scoped_allocator_adaptor_base \ + : public OuterAlloc \ +{ \ + typedef allocator_traits outer_traits_type; \ + BOOST_COPYABLE_AND_MOVABLE(scoped_allocator_adaptor_base) \ + \ + public: \ + template \ + struct rebind_base \ + { \ + typedef scoped_allocator_adaptor_base other; \ + }; \ + \ + typedef OuterAlloc outer_allocator_type; \ + typedef scoped_allocator_adaptor inner_allocator_type; \ + typedef scoped_allocator_adaptor scoped_allocator_type; \ + typedef allocator_traits inner_traits_type; \ + typedef boost::integral_constant< \ + bool, \ + outer_traits_type::propagate_on_container_copy_assignment::value || \ + inner_allocator_type::propagate_on_container_copy_assignment::value \ + > propagate_on_container_copy_assignment; \ + typedef boost::integral_constant< \ + bool, \ + outer_traits_type::propagate_on_container_move_assignment::value || \ + inner_allocator_type::propagate_on_container_move_assignment::value \ + > propagate_on_container_move_assignment; \ + typedef boost::integral_constant< \ + bool, \ + outer_traits_type::propagate_on_container_swap::value || \ + inner_allocator_type::propagate_on_container_swap::value \ + > propagate_on_container_swap; \ + \ + scoped_allocator_adaptor_base() \ + {} \ + \ + template \ + scoped_allocator_adaptor_base(BOOST_FWD_REF(OuterA2) outerAlloc \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_CONST_REF_PARAM_LIST_Q, _)) \ + : outer_allocator_type(::boost::forward(outerAlloc)) \ + , m_inner(BOOST_PP_ENUM_PARAMS(n, q)) \ + {} \ + \ + scoped_allocator_adaptor_base(const scoped_allocator_adaptor_base& other) \ + : outer_allocator_type(other.outer_allocator()) \ + , m_inner(other.inner_allocator()) \ + {} \ + \ + scoped_allocator_adaptor_base(BOOST_RV_REF(scoped_allocator_adaptor_base) other) \ + : outer_allocator_type(::boost::move(other.outer_allocator())) \ + , m_inner(::boost::move(other.inner_allocator())) \ + {} \ + \ + template \ + scoped_allocator_adaptor_base(const scoped_allocator_adaptor_base& other) \ + : outer_allocator_type(other.outer_allocator()) \ + , m_inner(other.inner_allocator()) \ + {} \ + \ + template \ + scoped_allocator_adaptor_base \ + (BOOST_RV_REF_BEG scoped_allocator_adaptor_base BOOST_RV_REF_END other) \ + : outer_allocator_type(other.outer_allocator()) \ + , m_inner(other.inner_allocator()) \ + {} \ + \ + public: \ + struct internal_type_t{}; \ + \ + template \ + scoped_allocator_adaptor_base \ + ( internal_type_t \ + , BOOST_FWD_REF(OuterA2) outerAlloc \ + , const inner_allocator_type &inner) \ + : outer_allocator_type(::boost::forward(outerAlloc)) \ + , m_inner(inner) \ + {} \ + \ + public: \ + scoped_allocator_adaptor_base &operator= \ + (BOOST_COPY_ASSIGN_REF(scoped_allocator_adaptor_base) other) \ + { \ + outer_allocator_type::operator=(other.outer_allocator()); \ + m_inner = other.inner_allocator(); \ + return *this; \ + } \ + \ + scoped_allocator_adaptor_base &operator=(BOOST_RV_REF(scoped_allocator_adaptor_base) other) \ + { \ + outer_allocator_type::operator=(boost::move(other.outer_allocator())); \ + m_inner = ::boost::move(other.inner_allocator()); \ + return *this; \ + } \ + \ + void swap(scoped_allocator_adaptor_base &r) \ + { \ + boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator()); \ + boost::container::swap_dispatch(this->m_inner, r.inner_allocator()); \ + } \ + \ + friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r) \ + { l.swap(r); } \ + \ + inner_allocator_type& inner_allocator() \ + { return m_inner; } \ + \ + inner_allocator_type const& inner_allocator() const \ + { return m_inner; } \ + \ + outer_allocator_type & outer_allocator() \ + { return static_cast(*this); } \ + \ + const outer_allocator_type &outer_allocator() const \ + { return static_cast(*this); } \ + \ + scoped_allocator_type select_on_container_copy_construction() const \ + { \ + return scoped_allocator_type \ + (internal_type_t() \ + ,outer_traits_type::select_on_container_copy_construction(this->outer_allocator()) \ + ,inner_traits_type::select_on_container_copy_construction(this->inner_allocator()) \ + ); \ + } \ + private: \ + inner_allocator_type m_inner; \ +}; \ +//! +#define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) +#include BOOST_PP_LOCAL_ITERATE() + +#endif //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +//Specialization for adaptor without any InnerAlloc +template +class scoped_allocator_adaptor_base + < OuterAlloc + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + , true + BOOST_PP_ENUM_TRAILING(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, BOOST_CONTAINER_PP_IDENTITY, nat) + #endif + > + : public OuterAlloc +{ + BOOST_COPYABLE_AND_MOVABLE(scoped_allocator_adaptor_base) + public: + + template + struct rebind_base + { + typedef scoped_allocator_adaptor_base + ::template portable_rebind_alloc::type + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + , true + BOOST_PP_ENUM_TRAILING(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, BOOST_CONTAINER_PP_IDENTITY, container_detail::nat) + #endif + > other; + }; + + typedef OuterAlloc outer_allocator_type; + typedef allocator_traits outer_traits_type; + typedef scoped_allocator_adaptor inner_allocator_type; + typedef inner_allocator_type scoped_allocator_type; + typedef allocator_traits inner_traits_type; + typedef typename outer_traits_type:: + propagate_on_container_copy_assignment propagate_on_container_copy_assignment; + typedef typename outer_traits_type:: + propagate_on_container_move_assignment propagate_on_container_move_assignment; + typedef typename outer_traits_type:: + propagate_on_container_swap propagate_on_container_swap; + + scoped_allocator_adaptor_base() + {} + + template + scoped_allocator_adaptor_base(BOOST_FWD_REF(OuterA2) outerAlloc) + : outer_allocator_type(::boost::forward(outerAlloc)) + {} + + scoped_allocator_adaptor_base(const scoped_allocator_adaptor_base& other) + : outer_allocator_type(other.outer_allocator()) + {} + + scoped_allocator_adaptor_base(BOOST_RV_REF(scoped_allocator_adaptor_base) other) + : outer_allocator_type(::boost::move(other.outer_allocator())) + {} + + template + scoped_allocator_adaptor_base + (const scoped_allocator_adaptor_base< + OuterA2 + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + , true + BOOST_PP_ENUM_TRAILING(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, BOOST_CONTAINER_PP_IDENTITY, container_detail::nat) + #endif + >& other) + : outer_allocator_type(other.outer_allocator()) + {} + + template + scoped_allocator_adaptor_base + (BOOST_RV_REF_BEG scoped_allocator_adaptor_base< + OuterA2 + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + , true + BOOST_PP_ENUM_TRAILING(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, BOOST_CONTAINER_PP_IDENTITY, container_detail::nat) + #endif + > BOOST_RV_REF_END other) + : outer_allocator_type(other.outer_allocator()) + {} + + public: + struct internal_type_t{}; + + template + scoped_allocator_adaptor_base(internal_type_t, BOOST_FWD_REF(OuterA2) outerAlloc, const inner_allocator_type &) + : outer_allocator_type(::boost::forward(outerAlloc)) + {} + + public: + scoped_allocator_adaptor_base &operator=(BOOST_COPY_ASSIGN_REF(scoped_allocator_adaptor_base) other) + { + outer_allocator_type::operator=(other.outer_allocator()); + return *this; + } + + scoped_allocator_adaptor_base &operator=(BOOST_RV_REF(scoped_allocator_adaptor_base) other) + { + outer_allocator_type::operator=(boost::move(other.outer_allocator())); + return *this; + } + + void swap(scoped_allocator_adaptor_base &r) + { + boost::container::swap_dispatch(this->outer_allocator(), r.outer_allocator()); + } + + friend void swap(scoped_allocator_adaptor_base &l, scoped_allocator_adaptor_base &r) + { l.swap(r); } + + inner_allocator_type& inner_allocator() + { return static_cast(*this); } + + inner_allocator_type const& inner_allocator() const + { return static_cast(*this); } + + outer_allocator_type & outer_allocator() + { return static_cast(*this); } + + const outer_allocator_type &outer_allocator() const + { return static_cast(*this); } + + scoped_allocator_type select_on_container_copy_construction() const + { + return scoped_allocator_type + (internal_type_t() + ,outer_traits_type::select_on_container_copy_construction(this->outer_allocator()) + //Don't use inner_traits_type::select_on_container_copy_construction(this->inner_allocator()) + //as inner_allocator() is equal to *this and that would trigger an infinite loop + , this->inner_allocator() + ); + } +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//Scoped allocator +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + + //! This class is a C++03-compatible implementation of std::scoped_allocator_adaptor. + //! The class template scoped_allocator_adaptor is an allocator template that specifies + //! the memory resource (the outer allocator) to be used by a container (as any other + //! allocator does) and also specifies an inner allocator resource to be passed to + //! the constructor of every element within the container. + //! + //! This adaptor is + //! instantiated with one outer and zero or more inner allocator types. If + //! instantiated with only one allocator type, the inner allocator becomes the + //! scoped_allocator_adaptor itself, thus using the same allocator resource for the + //! container and every element within the container and, if the elements themselves + //! are containers, each of their elements recursively. If instantiated with more than + //! one allocator, the first allocator is the outer allocator for use by the container, + //! the second allocator is passed to the constructors of the container's elements, + //! and, if the elements themselves are containers, the third allocator is passed to + //! the elements' elements, and so on. If containers are nested to a depth greater + //! than the number of allocators, the last allocator is used repeatedly, as in the + //! single-allocator case, for any remaining recursions. + //! + //! [Note: The + //! scoped_allocator_adaptor is derived from the outer allocator type so it can be + //! substituted for the outer allocator type in most expressions. -end note] + //! + //! In the construct member functions, OUTERMOST(x) is x if x does not have + //! an outer_allocator() member function and + //! OUTERMOST(x.outer_allocator()) otherwise; OUTERMOST_ALLOC_TRAITS(x) is + //! allocator_traits. + //! + //! [Note: OUTERMOST(x) and + //! OUTERMOST_ALLOC_TRAITS(x) are recursive operations. It is incumbent upon + //! the definition of outer_allocator() to ensure that the recursion terminates. + //! It will terminate for all instantiations of scoped_allocator_adaptor. -end note] + template + class scoped_allocator_adaptor + + #else // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + + template + class scoped_allocator_adaptor + + #endif // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + +#else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + +template +class scoped_allocator_adaptor +#endif + : public container_detail::scoped_allocator_adaptor_base + +{ + BOOST_COPYABLE_AND_MOVABLE(scoped_allocator_adaptor) + + public: + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef container_detail::scoped_allocator_adaptor_base + base_type; + typedef typename base_type::internal_type_t internal_type_t; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef OuterAlloc outer_allocator_type; + //! Type: For exposition only + //! + typedef allocator_traits outer_traits_type; + //! Type: scoped_allocator_adaptor if sizeof...(InnerAllocs) is zero; otherwise, + //! scoped_allocator_adaptor. + typedef typename base_type::inner_allocator_type inner_allocator_type; + typedef allocator_traits inner_traits_type; + typedef typename outer_traits_type::value_type value_type; + typedef typename outer_traits_type::size_type size_type; + typedef typename outer_traits_type::difference_type difference_type; + typedef typename outer_traits_type::pointer pointer; + typedef typename outer_traits_type::const_pointer const_pointer; + typedef typename outer_traits_type::void_pointer void_pointer; + typedef typename outer_traits_type::const_void_pointer const_void_pointer; + //! Type: true_type if allocator_traits::propagate_on_container_copy_assignment::value is + //! true for any Allocator in the set of OuterAlloc and InnerAllocs...; otherwise, false_type. + typedef typename base_type:: + propagate_on_container_copy_assignment propagate_on_container_copy_assignment; + //! Type: true_type if allocator_traits::propagate_on_container_move_assignment::value is + //! true for any Allocator in the set of OuterAlloc and InnerAllocs...; otherwise, false_type. + typedef typename base_type:: + propagate_on_container_move_assignment propagate_on_container_move_assignment; + //! Type: true_type if allocator_traits::propagate_on_container_swap::value is true for any + //! Allocator in the set of OuterAlloc and InnerAllocs...; otherwise, false_type. + typedef typename base_type:: + propagate_on_container_swap propagate_on_container_swap; + + //! Type: Rebinds scoped allocator to + //! typedef scoped_allocator_adaptor + //! < typename outer_traits_type::template portable_rebind_alloc::type + //! , InnerAllocs... > + template + struct rebind + { + typedef scoped_allocator_adaptor + < typename outer_traits_type::template portable_rebind_alloc::type + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , InnerAllocs... + #else + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS, Q) + #endif + > other; + }; + + //! Effects: value-initializes the OuterAlloc base class + //! and the inner allocator object. + scoped_allocator_adaptor() + {} + + ~scoped_allocator_adaptor() + {} + + //! Effects: initializes each allocator within the adaptor with + //! the corresponding allocator from other. + scoped_allocator_adaptor(const scoped_allocator_adaptor& other) + : base_type(other.base()) + {} + + //! Effects: move constructs each allocator within the adaptor with + //! the corresponding allocator from other. + scoped_allocator_adaptor(BOOST_RV_REF(scoped_allocator_adaptor) other) + : base_type(::boost::move(other.base())) + {} + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Requires: OuterAlloc shall be constructible from OuterA2. + //! + //! Effects: initializes the OuterAlloc base class with boost::forward(outerAlloc) and inner + //! with innerAllocs...(hence recursively initializing each allocator within the adaptor with the + //! corresponding allocator from the argument list). + template + scoped_allocator_adaptor(BOOST_FWD_REF(OuterA2) outerAlloc, const InnerAllocs & ...innerAllocs) + : base_type(::boost::forward(outerAlloc), innerAllocs...) + {} + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #define BOOST_PP_LOCAL_MACRO(n) \ + template \ + scoped_allocator_adaptor(BOOST_FWD_REF(OuterA2) outerAlloc \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_CONST_REF_PARAM_LIST_Q, _)) \ + : base_type(::boost::forward(outerAlloc) \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, q) \ + ) \ + {} \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Requires: OuterAlloc shall be constructible from OuterA2. + //! + //! Effects: initializes each allocator within the adaptor with the corresponding allocator from other. + template + scoped_allocator_adaptor(const scoped_allocator_adaptor &other) + : base_type(other.base()) + {} + + //! Requires: OuterAlloc shall be constructible from OuterA2. + //! + //! Effects: initializes each allocator within the adaptor with the corresponding allocator + //! rvalue from other. + template + scoped_allocator_adaptor(BOOST_RV_REF_BEG scoped_allocator_adaptor BOOST_RV_REF_END other) + : base_type(::boost::move(other.base())) + {} + + scoped_allocator_adaptor &operator=(BOOST_COPY_ASSIGN_REF(scoped_allocator_adaptor) other) + { return static_cast(base_type::operator=(static_cast(other))); } + + scoped_allocator_adaptor &operator=(BOOST_RV_REF(scoped_allocator_adaptor) other) + { return static_cast(base_type::operator=(boost::move(static_cast(other)))); } + + #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + //! Effects: swaps *this with r. + //! + void swap(scoped_allocator_adaptor &r); + + //! Effects: swaps *this with r. + //! + friend void swap(scoped_allocator_adaptor &l, scoped_allocator_adaptor &r); + + //! Returns: + //! static_cast(*this). + outer_allocator_type & outer_allocator() BOOST_CONTAINER_NOEXCEPT; + + //! Returns: + //! static_cast(*this). + const outer_allocator_type &outer_allocator() const BOOST_CONTAINER_NOEXCEPT; + + //! Returns: + //! *this if sizeof...(InnerAllocs) is zero; otherwise, inner. + inner_allocator_type& inner_allocator() BOOST_CONTAINER_NOEXCEPT; + + //! Returns: + //! *this if sizeof...(InnerAllocs) is zero; otherwise, inner. + inner_allocator_type const& inner_allocator() const BOOST_CONTAINER_NOEXCEPT; + + #endif //BOOST_CONTAINER_DOXYGEN_INVOKED + + //! Returns: + //! allocator_traits::max_size(outer_allocator()). + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { + return outer_traits_type::max_size(this->outer_allocator()); + } + + //! Effects: + //! calls OUTERMOST_ALLOC_TRAITS(*this)::destroy(OUTERMOST(*this), p). + template + void destroy(T* p) BOOST_CONTAINER_NOEXCEPT + { + allocator_traits::type> + ::destroy(get_outermost_allocator(this->outer_allocator()), p); + } + + //! Returns: + //! allocator_traits::allocate(outer_allocator(), n). + pointer allocate(size_type n) + { + return outer_traits_type::allocate(this->outer_allocator(), n); + } + + //! Returns: + //! allocator_traits::allocate(outer_allocator(), n, hint). + pointer allocate(size_type n, const_void_pointer hint) + { + return outer_traits_type::allocate(this->outer_allocator(), n, hint); + } + + //! Effects: + //! allocator_traits::deallocate(outer_allocator(), p, n). + void deallocate(pointer p, size_type n) + { + outer_traits_type::deallocate(this->outer_allocator(), p, n); + } + + #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + //! Returns: Allocator new scoped_allocator_adaptor object where each allocator + //! A in the adaptor is initialized from the result of calling + //! allocator_traits::select_on_container_copy_construction() on + //! the corresponding allocator in *this. + scoped_allocator_adaptor select_on_container_copy_construction() const; + #endif //BOOST_CONTAINER_DOXYGEN_INVOKED + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + base_type &base() { return *this; } + + const base_type &base() const { return *this; } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: + //! 1) If uses_allocator::value is false calls + //! OUTERMOST_ALLOC_TRAITS(*this)::construct + //! (OUTERMOST(*this), p, std::forward(args)...). + //! + //! 2) Otherwise, if uses_allocator::value is true and + //! is_constructible::value is true, calls + //! OUTERMOST_ALLOC_TRAITS(*this)::construct(OUTERMOST(*this), p, allocator_arg, + //! inner_allocator(), std::forward(args)...). + //! + //! [Note: In compilers without advanced decltype SFINAE support, is_constructible can't + //! be implemented so that condition will be replaced by + //! constructible_with_allocator_prefix::value. -end note] + //! + //! 3) Otherwise, if uses_allocator::value is true and + //! is_constructible::value is true, calls + //! OUTERMOST_ALLOC_TRAITS(*this)::construct(OUTERMOST(*this), p, + //! std::forward(args)..., inner_allocator()). + //! + //! [Note: In compilers without advanced decltype SFINAE support, is_constructible can't be + //! implemented so that condition will be replaced by + //! constructible_with_allocator_suffix::value. -end note] + //! + //! 4) Otherwise, the program is ill-formed. + //! + //! [Note: An error will result if uses_allocator evaluates + //! to true but the specific constructor does not take an allocator. This definition prevents a silent + //! failure to pass an inner allocator to a contained element. -end note] + template < typename T, class ...Args> + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + void + #else + typename container_detail::enable_if_c::value, void>::type + #endif + construct(T* p, BOOST_FWD_REF(Args)...args) + { + container_detail::dispatch_uses_allocator + ( uses_allocator() + , get_outermost_allocator(this->outer_allocator()) + , this->inner_allocator() + , p, ::boost::forward(args)...); + } + + #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //Disable this overload if the first argument is pair as some compilers have + //overload selection problems when the first parameter is a pair. + #define BOOST_PP_LOCAL_MACRO(n) \ + template < typename T \ + BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) \ + > \ + typename container_detail::enable_if_c::value, void>::type \ + construct(T* p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + container_detail::dispatch_uses_allocator \ + ( uses_allocator() \ + , get_outermost_allocator(this->outer_allocator()) \ + , this->inner_allocator() \ + , p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + template + void construct(std::pair* p) + { this->construct_pair(p); } + + template + void construct(container_detail::pair* p) + { this->construct_pair(p); } + + template + void construct(std::pair* p, BOOST_FWD_REF(U) x, BOOST_FWD_REF(V) y) + { this->construct_pair(p, ::boost::forward(x), ::boost::forward(y)); } + + template + void construct(container_detail::pair* p, BOOST_FWD_REF(U) x, BOOST_FWD_REF(V) y) + { this->construct_pair(p, ::boost::forward(x), ::boost::forward(y)); } + + template + void construct(std::pair* p, const std::pair& x) + { this->construct_pair(p, x); } + + template + void construct( container_detail::pair* p + , const container_detail::pair& x) + { this->construct_pair(p, x); } + + template + void construct( std::pair* p + , BOOST_RV_REF_BEG std::pair BOOST_RV_REF_END x) + { this->construct_pair(p, x); } + + template + void construct( container_detail::pair* p + , BOOST_RV_REF_BEG container_detail::pair BOOST_RV_REF_END x) + { this->construct_pair(p, x); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + template + void construct_pair(Pair* p) + { + this->construct(container_detail::addressof(p->first)); + BOOST_TRY{ + this->construct(container_detail::addressof(p->second)); + } + BOOST_CATCH(...){ + this->destroy(container_detail::addressof(p->first)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template + void construct_pair(Pair* p, BOOST_FWD_REF(U) x, BOOST_FWD_REF(V) y) + { + this->construct(container_detail::addressof(p->first), ::boost::forward(x)); + BOOST_TRY{ + this->construct(container_detail::addressof(p->second), ::boost::forward(y)); + } + BOOST_CATCH(...){ + this->destroy(container_detail::addressof(p->first)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template + void construct_pair(Pair* p, const Pair2& pr) + { + this->construct(container_detail::addressof(p->first), pr.first); + BOOST_TRY{ + this->construct(container_detail::addressof(p->second), pr.second); + } + BOOST_CATCH(...){ + this->destroy(container_detail::addressof(p->first)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template + void construct_pair(Pair* p, BOOST_RV_REF(Pair2) pr) + { + this->construct(container_detail::addressof(p->first), ::boost::move(pr.first)); + BOOST_TRY{ + this->construct(container_detail::addressof(p->second), ::boost::move(pr.second)); + } + BOOST_CATCH(...){ + this->destroy(container_detail::addressof(p->first)); + BOOST_RETHROW + } + BOOST_CATCH_END + } + + //template + //void construct(pair* p, piecewise_construct_t, tuple x, tuple y); + + public: + //Internal function + template + scoped_allocator_adaptor(internal_type_t, BOOST_FWD_REF(OuterA2) outer, const inner_allocator_type& inner) + : base_type(internal_type_t(), ::boost::forward(outer), inner) + {} + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +template +inline bool operator==( + const scoped_allocator_adaptor& a, + const scoped_allocator_adaptor& b) +{ + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + const bool has_zero_inner = sizeof...(InnerAllocs) == 0u; + #else + const bool has_zero_inner = + boost::container::container_detail::is_same + ::value; + #endif + + return a.outer_allocator() == b.outer_allocator() + && (has_zero_inner || a.inner_allocator() == b.inner_allocator()); +} + +template +inline bool operator!=( + const scoped_allocator_adaptor& a, + const scoped_allocator_adaptor& b) +{ + return ! (a == b); +} + +}} // namespace boost { namespace container { + +#include + +#endif // BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_HPP diff --git a/boost/container/scoped_allocator_fwd.hpp b/boost/container/scoped_allocator_fwd.hpp new file mode 100644 index 0000000..f19e27e --- /dev/null +++ b/boost/container/scoped_allocator_fwd.hpp @@ -0,0 +1,87 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_FWD_HPP +#define BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_FWD_HPP + +//! \file +//! This header file forward declares boost::container::scoped_allocator_adaptor +//! and defines the following types: + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include +#include +#endif + +namespace boost { namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + + template + class scoped_allocator_adaptor; + + #else // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + + template + class scoped_allocator_adaptor; + + template + class scoped_allocator_adaptor; + + #endif // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) + + +#else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +template +class scoped_allocator_adaptor; + +#endif + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! The allocator_arg_t struct is an empty structure type used as a unique type to +//! disambiguate constructor and function overloading. Specifically, several types +//! have constructors with allocator_arg_t as the first argument, immediately followed +//! by an argument of a type that satisfies the Allocator requirements +struct allocator_arg_t{}; + +//! A instance of type allocator_arg_t +//! +static const allocator_arg_t allocator_arg = allocator_arg_t(); + +template +struct constructible_with_allocator_suffix; + +template +struct constructible_with_allocator_prefix; + +template +struct uses_allocator; + +}} // namespace boost { namespace container { + +#include + +#endif // BOOST_CONTAINER_ALLOCATOR_SCOPED_ALLOCATOR_FWD_HPP diff --git a/boost/container/set.hpp b/boost/container/set.hpp new file mode 100644 index 0000000..3e2c2aa --- /dev/null +++ b/boost/container/set.hpp @@ -0,0 +1,1126 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_SET_HPP +#define BOOST_CONTAINER_SET_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#ifndef BOOST_CONTAINER_PERFECT_FORWARDING +#include +#endif +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +namespace boost { +namespace container { + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A set is a kind of associative container that supports unique keys (contains at +//! most one of each key value) and provides for fast retrieval of the keys themselves. +//! Class set supports bidirectional iterators. +//! +//! A set satisfies all of the requirements of a container and of a reversible container +//! , and of an associative container. A set also provides most operations described in +//! for unique keys. +//! +//! \tparam Key is the type to be inserted in the set, which is also the key_type +//! \tparam Compare is the comparison functor used to order keys +//! \tparam Allocator is the allocator to be used to allocate memory for this container +//! \tparam SetOptions is an packed option type generated using using boost::container::tree_assoc_options. +template , class Allocator = std::allocator, class SetOptions = tree_assoc_defaults > +#else +template +#endif +class set + ///@cond + : public container_detail::tree + < Key, Key, container_detail::identity, Compare, Allocator, SetOptions> + ///@endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(set) + typedef container_detail::tree + < Key, Key, container_detail::identity, Compare, Allocator, SetOptions> base_t; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef Key value_type; + typedef Compare key_compare; + typedef Compare value_compare; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs an empty set. + //! + //! Complexity: Constant. + set() + : base_t() + {} + + //! Effects: Constructs an empty set using the specified comparison object + //! and allocator. + //! + //! Complexity: Constant. + explicit set(const Compare& comp, + const allocator_type& a = allocator_type()) + : base_t(comp, a) + {} + + //! Effects: Constructs an empty set using the specified allocator object. + //! + //! Complexity: Constant. + explicit set(const allocator_type& a) + : base_t(a) + {} + + //! Effects: Constructs an empty set using the specified comparison object and + //! allocator, and inserts elements from the range [first ,last ). + //! + //! Complexity: Linear in N if the range [first ,last ) is already sorted using + //! comp and otherwise N logN, where N is last - first. + template + set(InputIterator first, InputIterator last, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(true, first, last, comp, a) + {} + + //! Effects: Constructs an empty set using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [first ,last). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + set( ordered_unique_range_t, InputIterator first, InputIterator last + , const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty set using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is il.begin() - il.end(). + set(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(true, il.begin(), il.end(), comp, a) + {} + + //! Effects: Constructs an empty set using the specified comparison object and + //! allocator, and inserts elements from the ordered unique range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + set(ordered_unique_range_t, std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + {} +#endif + + //! Effects: Copy constructs a set. + //! + //! Complexity: Linear in x.size(). + set(const set& x) + : base_t(static_cast(x)) + {} + + //! Effects: Move constructs a set. Constructs *this using x's resources. + //! + //! Complexity: Constant. + //! + //! Postcondition: x is emptied. + set(BOOST_RV_REF(set) x) + : base_t(boost::move(static_cast(x))) + {} + + //! Effects: Copy constructs a set using the specified allocator. + //! + //! Complexity: Linear in x.size(). + set(const set& x, const allocator_type &a) + : base_t(static_cast(x), a) + {} + + //! Effects: Move constructs a set using the specified allocator. + //! Constructs *this using x's resources. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + set(BOOST_RV_REF(set) x, const allocator_type &a) + : base_t(boost::move(static_cast(x)), a) + {} + + //! Effects: Makes *this a copy of x. + //! + //! Complexity: Linear in x.size(). + set& operator=(BOOST_COPY_ASSIGN_REF(set) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! Effects: this->swap(x.get()). + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + set& operator=(BOOST_RV_REF(set) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { return static_cast(this->base_t::operator=(boost::move(static_cast(x)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + set& operator=(std::initializer_list il) + { + this->clear(); + insert(il.begin(), il.end()); + return *this; + } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Returns a copy of the Allocator that + //! was passed to the object's constructor. + //! + //! Complexity: Constant. + allocator_type get_allocator() const; + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator(); + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const; + + //! Effects: Returns an iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant + iterator begin(); + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const; + + //! Effects: Returns a const_iterator to the first element contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const; + + //! Effects: Returns an iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end(); + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const; + + //! Effects: Returns a const_iterator to the end of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const; + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin(); + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const; + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend(); + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const; + + //! Effects: Returns true if the container contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const; + + //! Effects: Returns the number of the elements contained in the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const; + + //! Effects: Returns the largest possible size of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const; + #endif // #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object x of type Key constructed with + //! std::forward(args)... if and only if there is + //! no element in the container with equivalent value. + //! and returns the iterator pointing to the + //! newly inserted element. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Throws: If memory allocation throws or + //! Key's in-place constructor throws. + //! + //! Complexity: Logarithmic. + template + std::pair emplace(Args&&... args) + { return this->base_t::emplace_unique(boost::forward(args)...); } + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... if and only if there is + //! no element in the container with equivalent value. + //! p is a hint pointing to where the insert + //! should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_unique(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + std::pair emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); }\ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_unique(p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _));} \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts x if and only if there is no element in the container + //! with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(const value_type &x); + + //! Effects: Move constructs a new value from x if and only if there is + //! no element in the container with key equivalent to the key of x. + //! + //! Returns: The bool component of the returned pair is true if and only + //! if the insertion takes place, and the iterator component of the pair + //! points to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + std::pair insert(value_type &&x); + #else + private: + typedef std::pair insert_return_pair; + public: + BOOST_MOVE_CONVERSION_AWARE_CATCH(insert, value_type, insert_return_pair, this->priv_insert) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x in the container if and only if there is + //! no element in the container with key equivalent to the key of x. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, const value_type &x); + + //! Effects: Inserts an element move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent to the key of x. + //! + //! Complexity: Logarithmic. + iterator insert(const_iterator p, value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, value_type, iterator, this->priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_unique(first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(),il.end()) if and only + //! if there is no element with key equivalent to the key of that element. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.begin() to il.end()) + void insert(std::initializer_list il) + { this->base_t::insert_unique(il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Erases the element pointed to by p. + //! + //! Returns: Returns an iterator pointing to the element immediately + //! following q prior to the element being erased. If no such element exists, + //! returns end(). + //! + //! Complexity: Amortized constant time + iterator erase(const_iterator p); + + //! Effects: Erases all elements in the container with key equivalent to x. + //! + //! Returns: Returns the number of erased elements. + //! + //! Complexity: log(size()) + count(k) + size_type erase(const key_type& x); + + //! Effects: Erases all the elements in the range [first, last). + //! + //! Returns: Returns last. + //! + //! Complexity: log(size())+N where N is the distance from first to last. + iterator erase(const_iterator first, const_iterator last); + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(set& x); + + //! Effects: erase(a.begin(),a.end()). + //! + //! Postcondition: size() == 0. + //! + //! Complexity: linear in size(). + void clear(); + + //! Effects: Returns the comparison object out + //! of which a was constructed. + //! + //! Complexity: Constant. + key_compare key_comp() const; + + //! Effects: Returns an object of value_compare constructed out + //! of the comparison object. + //! + //! Complexity: Constant. + value_compare value_comp() const; + + //! Returns: An iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + iterator find(const key_type& x); + + //! Returns: Allocator const_iterator pointing to an element with the key + //! equivalent to x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic. + const_iterator find(const key_type& x) const; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) const + { return static_cast(this->base_t::find(x) != this->base_t::cend()); } + + //! Returns: The number of elements with key equivalent to x. + //! + //! Complexity: log(size())+count(k) + size_type count(const key_type& x) + { return static_cast(this->base_t::find(x) != this->base_t::end()); } + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Returns: An iterator pointing to the first element with key not less + //! than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator lower_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than k, or a.end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator lower_bound(const key_type& x) const; + + //! Returns: An iterator pointing to the first element with key not less + //! than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + iterator upper_bound(const key_type& x); + + //! Returns: Allocator const iterator pointing to the first element with key not + //! less than x, or end() if such an element is not found. + //! + //! Complexity: Logarithmic + const_iterator upper_bound(const key_type& x) const; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) + { return this->base_t::lower_bound_range(x); } + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const + { return this->base_t::lower_bound_range(x); } + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x); + + //! Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). + //! + //! Complexity: Logarithmic + std::pair equal_range(const key_type& x) const; + + //! Effects: Rebalances the tree. It's a no-op for Red-Black and AVL trees. + //! + //! Complexity: Linear + void rebalance(); + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const set& x, const set& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const set& x, const set& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const set& x, const set& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const set& x, const set& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const set& x, const set& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const set& x, const set& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(set& x, set& y); + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + template + std::pair priv_insert(BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_unique(::boost::forward(x)); } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_unique(p, ::boost::forward(x)); } + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A multiset is a kind of associative container that supports equivalent keys +//! (possibly contains multiple copies of the same key value) and provides for +//! fast retrieval of the keys themselves. Class multiset supports bidirectional iterators. +//! +//! A multiset satisfies all of the requirements of a container and of a reversible +//! container, and of an associative container). multiset also provides most operations +//! described for duplicate keys. +//! +//! \tparam Key is the type to be inserted in the set, which is also the key_type +//! \tparam Compare is the comparison functor used to order keys +//! \tparam Allocator is the allocator to be used to allocate memory for this container +//! \tparam MultiSetOptions is an packed option type generated using using boost::container::tree_assoc_options. +template , class Allocator = std::allocator, class MultiSetOptions = tree_assoc_defaults > +#else +template +#endif +class multiset + /// @cond + : public container_detail::tree + , Compare, Allocator, MultiSetOptions> + /// @endcond +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(multiset) + typedef container_detail::tree + , Compare, Allocator, MultiSetOptions> base_t; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Key key_type; + typedef Key value_type; + typedef Compare key_compare; + typedef Compare value_compare; + typedef ::boost::container::allocator_traits allocator_traits_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::stored_allocator_type) stored_allocator_type; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::iterator) iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_iterator) const_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::reverse_iterator) reverse_iterator; + typedef typename BOOST_CONTAINER_IMPDEF(base_t::const_reverse_iterator) const_reverse_iterator; + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! @copydoc ::boost::container::set::set() + multiset() + : base_t() + {} + + //! @copydoc ::boost::container::set::set(const Compare&, const allocator_type&) + explicit multiset(const Compare& comp, + const allocator_type& a = allocator_type()) + : base_t(comp, a) + {} + + //! @copydoc ::boost::container::set::set(const allocator_type&) + explicit multiset(const allocator_type& a) + : base_t(a) + {} + + //! @copydoc ::boost::container::set::set(InputIterator, InputIterator, const Compare& comp, const allocator_type&) + template + multiset(InputIterator first, InputIterator last, + const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : base_t(false, first, last, comp, a) + {} + + //! Effects: Constructs an empty multiset using the specified comparison object and + //! allocator, and inserts elements from the ordered range [first ,last ). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [first ,last) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + template + multiset( ordered_range_t, InputIterator first, InputIterator last + , const Compare& comp = Compare() + , const allocator_type& a = allocator_type()) + : base_t(ordered_range, first, last, comp, a) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @copydoc ::boost::container::set::set(std::initializer_list, const Compare& comp, const allocator_type&) + multiset(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(false, il.begin(), il.end(), comp, a) + {} + + //! @copydoc ::boost::container::set::set(ordered_unique_range_t, std::initializer_list, const Compare& comp, const allocator_type&) + multiset(ordered_unique_range_t, std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : base_t(ordered_range, il.begin(), il.end(), comp, a) + {} +#endif + + + //! @copydoc ::boost::container::set::set(const set &) + multiset(const multiset& x) + : base_t(static_cast(x)) + {} + + //! @copydoc ::boost::container::set(set &&) + multiset(BOOST_RV_REF(multiset) x) + : base_t(boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::container::set(const set &, const allocator_type &) + multiset(const multiset& x, const allocator_type &a) + : base_t(static_cast(x), a) + {} + + //! @copydoc ::boost::container::set(set &&, const allocator_type &) + multiset(BOOST_RV_REF(multiset) x, const allocator_type &a) + : base_t(boost::move(static_cast(x)), a) + {} + + //! @copydoc ::boost::container::set::operator=(const set &) + multiset& operator=(BOOST_COPY_ASSIGN_REF(multiset) x) + { return static_cast(this->base_t::operator=(static_cast(x))); } + + //! @copydoc ::boost::container::set::operator=(set &&) + multiset& operator=(BOOST_RV_REF(multiset) x) + { return static_cast(this->base_t::operator=(boost::move(static_cast(x)))); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @copydoc ::boost::container::set::operator=(std::initializer_list) + multiset& operator=(std::initializer_list il) + { + this->clear(); + insert(il.begin(), il.end()); + return *this; + } +#endif + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::set::get_allocator() + allocator_type get_allocator() const; + + //! @copydoc ::boost::container::set::get_stored_allocator() + stored_allocator_type &get_stored_allocator(); + + //! @copydoc ::boost::container::set::get_stored_allocator() const + const stored_allocator_type &get_stored_allocator() const; + + //! @copydoc ::boost::container::set::begin() + iterator begin(); + + //! @copydoc ::boost::container::set::begin() const + const_iterator begin() const; + + //! @copydoc ::boost::container::set::cbegin() const + const_iterator cbegin() const; + + //! @copydoc ::boost::container::set::end() + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::end() const + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::cend() const + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rbegin() + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rbegin() const + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::crbegin() const + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rend() + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::rend() const + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::crend() const + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::empty() const + bool empty() const; + + //! @copydoc ::boost::container::set::size() const + size_type size() const; + + //! @copydoc ::boost::container::set::max_size() const + size_type max_size() const; + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic. + template + iterator emplace(Args&&... args) + { return this->base_t::emplace_equal(boost::forward(args)...); } + + //! Effects: Inserts an object of type Key constructed with + //! std::forward(args)... + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + template + iterator emplace_hint(const_iterator p, Args&&... args) + { return this->base_t::emplace_hint_equal(p, boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_hint(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { return this->base_t::emplace_hint_equal(p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _));} \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts x and returns the iterator pointing to the + //! newly inserted element. + //! + //! Complexity: Logarithmic. + iterator insert(const value_type &x); + + //! Effects: Inserts a copy of x in the container. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(insert, value_type, iterator, this->priv_insert) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, const value_type &x); + + //! Effects: Inserts a value move constructed from x in the container. + //! p is a hint pointing to where the insert should start to search. + //! + //! Returns: An iterator pointing to the element with key equivalent + //! to the key of x. + //! + //! Complexity: Logarithmic in general, but amortized constant if t + //! is inserted right before p. + iterator insert(const_iterator p, value_type &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, value_type, iterator, this->priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: first, last are not iterators into *this. + //! + //! Effects: inserts each element from the range [first,last) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + template + void insert(InputIterator first, InputIterator last) + { this->base_t::insert_equal(first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @copydoc ::boost::container::set::insert(std::initializer_list) + void insert(std::initializer_list il) + { this->base_t::insert_unique(il.begin(), il.end()); } +#endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! @copydoc ::boost::container::set::erase(const_iterator) + iterator erase(const_iterator p); + + //! @copydoc ::boost::container::set::erase(const key_type&) + size_type erase(const key_type& x); + + //! @copydoc ::boost::container::set::erase(const_iterator,const_iterator) + iterator erase(const_iterator first, const_iterator last); + + //! @copydoc ::boost::container::set::swap + void swap(flat_multiset& x); + + //! @copydoc ::boost::container::set::clear + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! @copydoc ::boost::container::set::key_comp + key_compare key_comp() const; + + //! @copydoc ::boost::container::set::value_comp + value_compare value_comp() const; + + //! @copydoc ::boost::container::set::find(const key_type& ) + iterator find(const key_type& x); + + //! @copydoc ::boost::container::set::find(const key_type& ) const + const_iterator find(const key_type& x) const; + + //! @copydoc ::boost::container::set::count(const key_type& ) const + size_type count(const key_type& x) const; + + //! @copydoc ::boost::container::set::lower_bound(const key_type& ) + iterator lower_bound(const key_type& x); + + //! @copydoc ::boost::container::set::lower_bound(const key_type& ) const + const_iterator lower_bound(const key_type& x) const; + + //! @copydoc ::boost::container::set::upper_bound(const key_type& ) + iterator upper_bound(const key_type& x); + + //! @copydoc ::boost::container::set::upper_bound(const key_type& ) const + const_iterator upper_bound(const key_type& x) const; + + //! @copydoc ::boost::container::set::equal_range(const key_type& ) const + std::pair equal_range(const key_type& x) const; + + //! @copydoc ::boost::container::set::equal_range(const key_type& ) + std::pair equal_range(const key_type& x); + + //! @copydoc ::boost::container::set::rebalance() + void rebalance(); + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const multiset& x, const multiset& y); + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const multiset& x, const multiset& y); + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const multiset& x, const multiset& y); + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const multiset& x, const multiset& y); + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const multiset& x, const multiset& y); + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const multiset& x, const multiset& y); + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(multiset& x, multiset& y); + + #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + template + iterator priv_insert(BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_equal(::boost::forward(x)); } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(KeyType) x) + { return this->base_t::insert_equal(p, ::boost::forward(x)); } + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +} //namespace container { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > +{ + static const bool value = has_trivial_destructor_after_move::value && has_trivial_destructor_after_move::value; +}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} + +#include + +#endif /* BOOST_CONTAINER_SET_HPP */ + diff --git a/boost/container/slist.hpp b/boost/container/slist.hpp new file mode 100644 index 0000000..b6b4c38 --- /dev/null +++ b/boost/container/slist.hpp @@ -0,0 +1,1730 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2004-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_SLIST_HPP +#define BOOST_CONTAINER_SLIST_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) +//Preprocessor library to emulate perfect forwarding +#else +#include +#endif + +#include +#include +#include +#include +#include + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +template +class slist; + +namespace container_detail { + +template +struct slist_hook +{ + typedef typename container_detail::bi::make_slist_base_hook + , container_detail::bi::link_mode >::type type; +}; + +template +struct slist_node + : public slist_hook::type +{ + private: + slist_node(); + + public: + typedef T value_type; + typedef typename slist_hook::type hook_type; + + T m_data; + + T &get_data() + { return this->m_data; } + + const T &get_data() const + { return this->m_data; } +}; + +template +struct iiterator_node_value_type< slist_node > { + typedef T type; +}; + +template +struct intrusive_slist_type +{ + typedef boost::container::allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::value_type value_type; + typedef typename boost::intrusive::pointer_traits + ::template + rebind_pointer::type + void_pointer; + typedef typename container_detail::slist_node + node_type; + + typedef typename container_detail::bi::make_slist + ::type> + ,container_detail::bi::constant_time_size + , container_detail::bi::size_type + + >::type container_type; + typedef container_type type ; +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! An slist is a singly linked list: a list where each element is linked to the next +//! element, but not to the previous element. That is, it is a Sequence that +//! supports forward but not backward traversal, and (amortized) constant time +//! insertion and removal of elements. Slists, like lists, have the important +//! property that insertion and splicing do not invalidate iterators to list elements, +//! and that even removal invalidates only the iterators that point to the elements +//! that are removed. The ordering of iterators may be changed (that is, +//! slist::iterator might have a different predecessor or successor after a list +//! operation than it did before), but the iterators themselves will not be invalidated +//! or made to point to different elements unless that invalidation or mutation is explicit. +//! +//! The main difference between slist and list is that list's iterators are bidirectional +//! iterators, while slist's iterators are forward iterators. This means that slist is +//! less versatile than list; frequently, however, bidirectional iterators are +//! unnecessary. You should usually use slist unless you actually need the extra +//! functionality of list, because singly linked lists are smaller and faster than double +//! linked lists. +//! +//! Important performance note: like every other Sequence, slist defines the member +//! functions insert and erase. Using these member functions carelessly, however, can +//! result in disastrously slow programs. The problem is that insert's first argument is +//! an iterator p, and that it inserts the new element(s) before p. This means that +//! insert must find the iterator just before p; this is a constant-time operation +//! for list, since list has bidirectional iterators, but for slist it must find that +//! iterator by traversing the list from the beginning up to p. In other words: +//! insert and erase are slow operations anywhere but near the beginning of the slist. +//! +//! Slist provides the member functions insert_after and erase_after, which are constant +//! time operations: you should always use insert_after and erase_after whenever +//! possible. If you find that insert_after and erase_after aren't adequate for your +//! needs, and that you often need to use insert and erase in the middle of the list, +//! then you should probably use list instead of slist. +//! +//! \tparam T The type of object that is stored in the list +//! \tparam Allocator The allocator used for all internal memory management +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template > +#else +template +#endif +class slist + : protected container_detail::node_alloc_holder + ::type> +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef typename + container_detail::intrusive_slist_type::type Icont; + typedef container_detail::node_alloc_holder AllocHolder; + typedef typename AllocHolder::NodePtr NodePtr; + typedef typename AllocHolder::NodeAlloc NodeAlloc; + typedef typename AllocHolder::ValAlloc ValAlloc; + typedef typename AllocHolder::Node Node; + typedef container_detail::allocator_destroyer Destroyer; + typedef typename AllocHolder::allocator_v1 allocator_v1; + typedef typename AllocHolder::allocator_v2 allocator_v2; + typedef typename AllocHolder::alloc_version alloc_version; + typedef boost::container::allocator_traits allocator_traits_type; + + class equal_to_value + { + typedef typename AllocHolder::value_type value_type; + const value_type &t_; + + public: + equal_to_value(const value_type &t) + : t_(t) + {} + + bool operator()(const value_type &t)const + { return t_ == t; } + }; + + template + struct ValueCompareToNodeCompare + : Pred + { + ValueCompareToNodeCompare(Pred pred) + : Pred(pred) + {} + + bool operator()(const Node &a, const Node &b) const + { return static_cast(*this)(a.m_data, b.m_data); } + + bool operator()(const Node &a) const + { return static_cast(*this)(a.m_data); } + }; + + BOOST_COPYABLE_AND_MOVABLE(slist) + typedef container_detail::iterator iterator_impl; + typedef container_detail::iterator const_iterator_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef T value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(NodeAlloc) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + + public: + + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Constructs a list taking the allocator as parameter. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant. + slist() + : AllocHolder() + {} + + //! Effects: Constructs a list taking the allocator as parameter. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + explicit slist(const allocator_type& a) BOOST_CONTAINER_NOEXCEPT + : AllocHolder(a) + {} + + explicit slist(size_type n) + : AllocHolder(allocator_type()) + { this->resize(n); } + + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + explicit slist(size_type n, const value_type& x, const allocator_type& a = allocator_type()) + : AllocHolder(a) + { this->insert_after(this->cbefore_begin(), n, x); } + + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts a copy of the range [first, last) in the list. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + slist(InpIt first, InpIt last, const allocator_type& a = allocator_type()) + : AllocHolder(a) + { this->insert_after(this->cbefore_begin(), first, last); } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a list that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.end()) in the list. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced std::initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + slist(std::initializer_list il, const allocator_type& a = allocator_type()) + : AllocHolder(a) + { this->insert_after(this->cbefore_begin(), il.begin(), il.end()); } +#endif + + //! Effects: Copy constructs a list. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor + //! + //! Complexity: Linear to the elements x contains. + slist(const slist& x) + : AllocHolder(x) + { this->insert_after(this->cbefore_begin(), x.begin(), x.end()); } + + //! Effects: Move constructor. Moves mx's resources to *this. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant. + slist(BOOST_RV_REF(slist) x) + : AllocHolder(boost::move(static_cast(x))) + {} + + //! Effects: Copy constructs a list using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor + //! + //! Complexity: Linear to the elements x contains. + slist(const slist& x, const allocator_type &a) + : AllocHolder(a) + { this->insert_after(this->cbefore_begin(), x.begin(), x.end()); } + + //! Effects: Move constructor using the specified allocator. + //! Moves x's resources to *this. + //! + //! Throws: If allocation or value_type's copy constructor throws. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + slist(BOOST_RV_REF(slist) x, const allocator_type &a) + : AllocHolder(a) + { + if(this->node_alloc() == x.node_alloc()){ + this->icont().swap(x.icont()); + } + else{ + this->insert_after(this->cbefore_begin(), x.begin(), x.end()); + } + } + + //! Effects: Destroys the list. All stored values are destroyed + //! and used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements. + ~slist() BOOST_CONTAINER_NOEXCEPT + {} //AllocHolder clears the slist + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in x. + slist& operator= (BOOST_COPY_ASSIGN_REF(slist) x) + { + if (&x != this){ + NodeAlloc &this_alloc = this->node_alloc(); + const NodeAlloc &x_alloc = x.node_alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + } + this->AllocHolder::copy_assign_alloc(x); + this->assign(x.begin(), x.end()); + } + return *this; + } + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + slist& operator= (BOOST_RV_REF(slist) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + BOOST_ASSERT(this != &x); + NodeAlloc &this_alloc = this->node_alloc(); + NodeAlloc &x_alloc = x.node_alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy + this->clear(); + //Move allocator if needed + this->AllocHolder::move_assign_alloc(x); + //Obtain resources + this->icont() = boost::move(x.icont()); + } + //Else do a one by one move + else{ + this->assign( boost::make_move_iterator(x.begin()) + , boost::make_move_iterator(x.end())); + } + return *this; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Makes *this contain the same elements as in il. + //! + //! Postcondition: this->size() == il.size(). *this contains a copy + //! of each of il's elements. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + slist& operator=(std::initializer_list il) + { + assign(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Assigns the n copies of val to *this. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + void assign(size_type n, const T& val) + { + typedef constant_iterator cvalue_iterator; + return this->assign(cvalue_iterator(val, n), cvalue_iterator()); + } + + //! Effects: Assigns the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing InpIt throws. + //! + //! Complexity: Linear to n. + template + void assign(InpIt first, InpIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + >::type * = 0 + #endif + ) + { + iterator end_n(this->end()); + iterator prev(this->before_begin()); + iterator node(this->begin()); + while (node != end_n && first != last){ + *node = *first; + prev = node; + ++node; + ++first; + } + if (first != last) + this->insert_after(prev, first, last); + else + this->erase_after(prev, end_n); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing std::initializer_list iterator throws. + //! + //! Complexity: Linear to range [il.begin(), il.end()). + + void assign(std::initializer_list il) + { + assign(il.begin(), il.end()); + } +#endif + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return allocator_type(this->node_alloc()); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return this->node_alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->node_alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns a non-dereferenceable iterator that, + //! when incremented, yields begin(). This iterator may be used + //! as the argument to insert_after, erase_after, etc. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator before_begin() BOOST_CONTAINER_NOEXCEPT + { return iterator(end()); } + + //! Effects: Returns a non-dereferenceable const_iterator + //! that, when incremented, yields begin(). This iterator may be used + //! as the argument to insert_after, erase_after, etc. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator before_begin() const BOOST_CONTAINER_NOEXCEPT + { return this->cbefore_begin(); } + + //! Effects: Returns an iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().begin()); } + + //! Effects: Returns a const_iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return this->cbegin(); } + + //! Effects: Returns an iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().end()); } + + //! Effects: Returns a const_iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return this->cend(); } + + //! Effects: Returns a non-dereferenceable const_iterator + //! that, when incremented, yields begin(). This iterator may be used + //! as the argument to insert_after, erase_after, etc. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbefore_begin() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(end()); } + + //! Effects: Returns a const_iterator to the first element contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->non_const_icont().begin()); } + + //! Effects: Returns a const_iterator to the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->non_const_icont().end()); } + + //! Returns: The iterator to the element before i in the sequence. + //! Returns the end-iterator, if either i is the begin-iterator or the + //! sequence is empty. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements before i. + //! + //! Note: Non-standard extension. + iterator previous(iterator p) BOOST_CONTAINER_NOEXCEPT + { return iterator(this->icont().previous(p.get())); } + + //! Returns: The const_iterator to the element before i in the sequence. + //! Returns the end-const_iterator, if either i is the begin-const_iterator or + //! the sequence is empty. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements before i. + //! + //! Note: Non-standard extension. + const_iterator previous(const_iterator p) + { return const_iterator(this->icont().previous(p.get())); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the list contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const + { return !this->size(); } + + //! Effects: Returns the number of the elements contained in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const + { return this->icont().size(); } + + //! Effects: Returns the largest possible size of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const + { return AllocHolder::max_size(); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size) + { + const_iterator last_pos; + if(!priv_try_shrink(new_size, last_pos)){ + typedef value_init_construct_iterator value_init_iterator; + this->insert_after(last_pos, value_init_iterator(new_size - this->size()), value_init_iterator()); + } + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size, const T& x) + { + const_iterator last_pos; + if(!priv_try_shrink(new_size, last_pos)){ + this->insert_after(last_pos, new_size, x); + } + } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() + { return *this->begin(); } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const + { return *this->begin(); } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the front of the list + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + template + void emplace_front(Args&&... args) + { this->emplace_after(this->cbefore_begin(), boost::forward(args)...); } + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... after prev + //! + //! Throws: If memory allocation throws or + //! T's in-place constructor throws. + //! + //! Complexity: Constant + template + iterator emplace_after(const_iterator prev, Args&&... args) + { + NodePtr pnode(AllocHolder::create_node(boost::forward(args)...)); + return iterator(this->icont().insert_after(prev.get(), *pnode)); + } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_front(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + this->emplace(this->cbegin() \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace_after(const_iterator prev \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + NodePtr pnode (AllocHolder::create_node \ + (BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ + return iterator(this->icont().insert_after(prev.get(), *pnode)); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the beginning of the list. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_front(const T &x); + + //! Effects: Constructs a new element in the beginning of the list + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_front(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front) + #endif + + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Inserts a copy of the value after prev_p. + //! + //! Returns: An iterator to the inserted element. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + //! + //! Note: Does not affect the validity of iterators and references of + //! previous values. + iterator insert_after(const_iterator prev_p, const T &x); + + //! Requires: prev_p must be a valid iterator of *this. + //! + //! Effects: Inserts a move constructed copy object from the value after the + //! p pointed by prev_p. + //! + //! Returns: An iterator to the inserted element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + //! + //! Note: Does not affect the validity of iterators and references of + //! previous values. + iterator insert_after(const_iterator prev_p, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_after, T, iterator, priv_insert_after, const_iterator, const_iterator) + #endif + + //! Requires: prev_p must be a valid iterator of *this. + //! + //! Effects: Inserts n copies of x after prev_p. + //! + //! Returns: an iterator to the last inserted element or prev_p if n is 0. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! + //! Complexity: Linear to n. + //! + //! Note: Does not affect the validity of iterators and references of + //! previous values. + iterator insert_after(const_iterator prev_p, size_type n, const value_type& x) + { + typedef constant_iterator cvalue_iterator; + return this->insert_after(prev_p, cvalue_iterator(x, n), cvalue_iterator()); + } + + //! Requires: prev_p must be a valid iterator of *this. + //! + //! Effects: Inserts the range pointed by [first, last) after prev_p. + //! + //! Returns: an iterator to the last inserted element or prev_p if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws. + //! + //! Complexity: Linear to the number of elements inserted. + //! + //! Note: Does not affect the validity of iterators and references of + //! previous values. + template + iterator insert_after(const_iterator prev_p, InpIt first, InpIt last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && (container_detail::is_input_iterator::value + || container_detail::is_same::value + ) + >::type * = 0 + #endif + ) + { + iterator ret_it(prev_p.get()); + for (; first != last; ++first){ + ret_it = iterator(this->icont().insert_after(ret_it.get(), *this->create_node_from_it(first))); + } + return ret_it; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: prev_p must be a valid iterator of *this. + //! + //! Effects: Inserts the range pointed by [il.begin(), il.end()) after prev_p. + //! + //! Returns: an iterator to the last inserted element or prev_p if il.begin() == il.end(). + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced std::initializer_list iterator throws. + //! + //! Complexity: Linear to the number of elements inserted. + //! + //! Note: Does not affect the validity of iterators and references of + //! previous values. + iterator insert_after(const_iterator prev_p, std::initializer_list il) + { + return insert_after(prev_p, il.begin(), il.end()); + } +#endif + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert_after(const_iterator prev, FwdIt first, FwdIt last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !(container_detail::is_input_iterator::value + || container_detail::is_same::value + ) + >::type * = 0 + ) + { + //Optimized allocation and construction + insertion_functor func(this->icont(), prev.get()); + this->allocate_many_and_construct(first, std::distance(first, last), func); + return iterator(func.inserted_first()); + } + #endif + + //! Effects: Removes the first element from the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Amortized constant time. + void pop_front() + { this->icont().pop_front_and_dispose(Destroyer(this->node_alloc())); } + + //! Effects: Erases the element after the element pointed by prev_p + //! of the list. + //! + //! Returns: the first element remaining beyond the removed elements, + //! or end() if no such element exists. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + //! + //! Note: Does not invalidate iterators or references to non erased elements. + iterator erase_after(const_iterator prev_p) + { + return iterator(this->icont().erase_after_and_dispose(prev_p.get(), Destroyer(this->node_alloc()))); + } + + //! Effects: Erases the range (before_first, last) from + //! the list. + //! + //! Returns: the first element remaining beyond the removed elements, + //! or end() if no such element exists. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of erased elements. + //! + //! Note: Does not invalidate iterators or references to non erased elements. + iterator erase_after(const_iterator before_first, const_iterator last) + { + return iterator(this->icont().erase_after_and_dispose(before_first.get(), last.get(), Destroyer(this->node_alloc()))); + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements on *this and x. + void swap(slist& x) + { AllocHolder::swap(x); } + + //! Effects: Erases all the elements of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the list. + void clear() + { this->icont().clear_and_dispose(Destroyer(this->node_alloc())); } + + ////////////////////////////////////////////// + // + // slist operations + // + ////////////////////////////////////////////// + + //! Requires: p must point to an element contained + //! by the list. x != *this + //! + //! Effects: Transfers all the elements of list x to this list, after the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: std::runtime_error if this' allocator and x's allocator + //! are not equal. + //! + //! Complexity: Linear to the elements in x. + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, slist& x) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this != &x); + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice_after(prev_p.get(), x.icont()); + } + + //! Requires: p must point to an element contained + //! by the list. x != *this + //! + //! Effects: Transfers all the elements of list x to this list, after the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: std::runtime_error if this' allocator and x's allocator + //! are not equal. + //! + //! Complexity: Linear to the elements in x. + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(prev_p, static_cast(x)); } + + //! Requires: prev_p must be a valid iterator of this. + //! i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! after the element pointed by prev_p. + //! If prev_p == prev or prev_p == ++prev, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, slist& x, const_iterator prev) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice_after(prev_p.get(), x.icont(), prev.get()); + } + + //! Requires: prev_p must be a valid iterator of this. + //! i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! after the element pointed by prev_p. + //! If prev_p == prev or prev_p == ++prev, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x, const_iterator prev) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(prev_p, static_cast(x), prev); } + + //! Requires: prev_p must be a valid iterator of this. + //! before_first and before_last must be valid iterators of x. + //! prev_p must not be contained in [before_first, before_last) range. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the range [before_first + 1, before_last + 1) + //! from list x to this list, after the element pointed by prev_p. + //! + //! Throws: Nothing + //! + //! Complexity: Linear to the number of transferred elements. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, slist& x, + const_iterator before_first, const_iterator before_last) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice_after + (prev_p.get(), x.icont(), before_first.get(), before_last.get()); + } + + //! Requires: prev_p must be a valid iterator of this. + //! before_first and before_last must be valid iterators of x. + //! prev_p must not be contained in [before_first, before_last) range. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the range [before_first + 1, before_last + 1) + //! from list x to this list, after the element pointed by prev_p. + //! + //! Throws: Nothing + //! + //! Complexity: Linear to the number of transferred elements. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x, + const_iterator before_first, const_iterator before_last) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(prev_p, static_cast(x), before_first, before_last); } + + //! Requires: prev_p must be a valid iterator of this. + //! before_first and before_last must be valid iterators of x. + //! prev_p must not be contained in [before_first, before_last) range. + //! n == std::distance(before_first, before_last). + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the range [before_first + 1, before_last + 1) + //! from list x to this list, after the element pointed by prev_p. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, slist& x, + const_iterator before_first, const_iterator before_last, + size_type n) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().splice_after + (prev_p.get(), x.icont(), before_first.get(), before_last.get(), n); + } + + //! Requires: prev_p must be a valid iterator of this. + //! before_first and before_last must be valid iterators of x. + //! prev_p must not be contained in [before_first, before_last) range. + //! n == std::distance(before_first, before_last). + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the range [before_first + 1, before_last + 1) + //! from list x to this list, after the element pointed by prev_p. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice_after(const_iterator prev_p, BOOST_RV_REF(slist) x, + const_iterator before_first, const_iterator before_last, + size_type n) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(prev_p, static_cast(x), before_first, before_last, n); } + + //! Effects: Removes all the elements that compare equal to value. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear time. It performs exactly size() comparisons for equality. + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + void remove(const T& value) + { this->remove_if(equal_to_value(value)); } + + //! Effects: Removes all the elements for which a specified + //! predicate is satisfied. + //! + //! Throws: If pred throws. + //! + //! Complexity: Linear time. It performs exactly size() calls to the predicate. + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + template + void remove_if(Pred pred) + { + typedef ValueCompareToNodeCompare Predicate; + this->icont().remove_and_dispose_if(Predicate(pred), Destroyer(this->node_alloc())); + } + + //! Effects: Removes adjacent duplicate elements or adjacent + //! elements that are equal from the list. + //! + //! Throws: If comparison throws. + //! + //! Complexity: Linear time (size()-1 comparisons equality comparisons). + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + void unique() + { this->unique(value_equal()); } + + //! Effects: Removes adjacent duplicate elements or adjacent + //! elements that satisfy some binary predicate from the list. + //! + //! Throws: If pred throws. + //! + //! Complexity: Linear time (size()-1 comparisons calls to pred()). + //! + //! Note: The relative order of elements that are not removed is unchanged, + //! and iterators to elements that are not removed remain valid. + template + void unique(Pred pred) + { + typedef ValueCompareToNodeCompare Predicate; + this->icont().unique_and_dispose(Predicate(pred), Destroyer(this->node_alloc())); + } + + //! Requires: The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this according to std::less. The merge is stable; + //! that is, if an element from *this is equivalent to one from x, then the element + //! from *this will precede the one from x. + //! + //! Throws: If comparison throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + void merge(slist & x) + { this->merge(x, value_less()); } + + //! Requires: The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this according to std::less. The merge is stable; + //! that is, if an element from *this is equivalent to one from x, then the element + //! from *this will precede the one from x. + //! + //! Throws: If comparison throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + void merge(BOOST_RV_REF(slist) x) + { this->merge(static_cast(x)); } + + //! Requires: p must be a comparison function that induces a strict weak + //! ordering and both *this and x must be sorted according to that ordering + //! The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this. The merge is stable; that is, if an element from *this is + //! equivalent to one from x, then the element from *this will precede the one from x. + //! + //! Throws: If comp throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + //! + //! Note: Iterators and references to *this are not invalidated. + template + void merge(slist& x, StrictWeakOrdering comp) + { + BOOST_ASSERT(this->node_alloc() == x.node_alloc()); + this->icont().merge(x.icont(), + ValueCompareToNodeCompare(comp)); + } + + //! Requires: p must be a comparison function that induces a strict weak + //! ordering and both *this and x must be sorted according to that ordering + //! The lists x and *this must be distinct. + //! + //! Effects: This function removes all of x's elements and inserts them + //! in order into *this. The merge is stable; that is, if an element from *this is + //! equivalent to one from x, then the element from *this will precede the one from x. + //! + //! Throws: If comp throws. + //! + //! Complexity: This function is linear time: it performs at most + //! size() + x.size() - 1 comparisons. + //! + //! Note: Iterators and references to *this are not invalidated. + template + void merge(BOOST_RV_REF(slist) x, StrictWeakOrdering comp) + { this->merge(static_cast(x), comp); } + + //! Effects: This function sorts the list *this according to std::less. + //! The sort is stable, that is, the relative order of equivalent elements is preserved. + //! + //! Throws: If comparison throws. + //! + //! Notes: Iterators and references are not invalidated. + //! + //! Complexity: The number of comparisons is approximately N log N, where N + //! is the list's size. + void sort() + { this->sort(value_less()); } + + //! Effects: This function sorts the list *this according to std::less. + //! The sort is stable, that is, the relative order of equivalent elements is preserved. + //! + //! Throws: If comp throws. + //! + //! Notes: Iterators and references are not invalidated. + //! + //! Complexity: The number of comparisons is approximately N log N, where N + //! is the list's size. + template + void sort(StrictWeakOrdering comp) + { + // nothing if the slist has length 0 or 1. + if (this->size() < 2) + return; + this->icont().sort(ValueCompareToNodeCompare(comp)); + } + + //! Effects: Reverses the order of elements in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: This function is linear time. + //! + //! Note: Iterators and references are not invalidated + void reverse() BOOST_CONTAINER_NOEXCEPT + { this->icont().reverse(); } + + ////////////////////////////////////////////// + // + // list compatibility interface + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... before p + //! + //! Throws: If memory allocation throws or + //! T's in-place constructor throws. + //! + //! Complexity: Linear to the elements before p + template + iterator emplace(const_iterator p, Args&&... args) + { return this->emplace_after(this->previous(p), boost::forward(args)...); } + + #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace (const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + return this->emplace_after \ + (this->previous(p) \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of x before p. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws or x's copy constructor throws. + //! + //! Complexity: Linear to the elements before p. + iterator insert(const_iterator p, const T &x); + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a new element before p with mx's resources. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Linear to the elements before p. + iterator insert(const_iterator prev_p, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Inserts n copies of x before p. + //! + //! Returns: an iterator to the first inserted element or p if n == 0. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n plus linear to the elements before p. + iterator insert(const_iterator p, size_type n, const value_type& x) + { + const_iterator prev(this->previous(p)); + this->insert_after(prev, n, x); + return ++iterator(prev.get()); + } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [first, last) range before p. + //! + //! Returns: an iterator to the first inserted element or p if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws. + //! + //! Complexity: Linear to std::distance [first, last) plus + //! linear to the elements before p. + template + iterator insert(const_iterator p, InIter first, InIter last) + { + const_iterator prev(this->previous(p)); + this->insert_after(prev, first, last); + return ++iterator(prev.get()); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before p. + //! + //! Returns: an iterator to the first inserted element or p if il.begin() == il.end(). + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced std::initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()) plus + //! linear to the elements before p. + iterator insert(const_iterator p, std::initializer_list il) + { + return insert(p, il.begin(), il.end()); + } +#endif + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Erases the element at p p. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements before p. + iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT + { return iterator(this->erase_after(previous(p))); } + + //! Requires: first and last must be valid iterator to elements in *this. + //! + //! Effects: Erases the elements pointed by [first, last). + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the distance between first and last plus + //! linear to the elements before first. + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { return iterator(this->erase_after(previous(first), last)); } + + //! Requires: p must point to an element contained + //! by the list. x != *this. this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers all the elements of list x to this list, before the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), and linear in x.size(). + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, slist& x) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(this->previous(p), x); } + + //! Requires: p must point to an element contained + //! by the list. x != *this. this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers all the elements of list x to this list, before the + //! the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), and linear in x.size(). + //! + //! Note: Iterators of values obtained from list x now point to elements of + //! this list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(slist) x) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x)); } + + //! Requires: p must point to an element contained + //! by this list. i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! If p == i or p == ++i, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), and in distance(x.begin(), i). + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, slist& x, const_iterator i) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(this->previous(p), x, this->previous(i)); } + + //! Requires: p must point to an element contained + //! by this list. i must point to an element contained in list x. + //! this' allocator and x's allocator shall compare equal. + //! + //! Effects: Transfers the value pointed by i, from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! If p == i or p == ++i, this function is a null operation. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), and in distance(x.begin(), i). + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(slist) x, const_iterator i) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x), i); } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! this' allocator and x's allocator shall compare equal. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), in distance(x.begin(), first), + //! and in distance(first, last). + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, slist& x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { this->splice_after(this->previous(p), x, this->previous(first), this->previous(last)); } + + //! Requires: p must point to an element contained + //! by this list. first and last must point to elements contained in list x. + //! this' allocator and x's allocator shall compare equal + //! + //! Effects: Transfers the range pointed by first and last from list x to this list, + //! before the the element pointed by p. No destructors or copy constructors are called. + //! + //! Throws: Nothing + //! + //! Complexity: Linear in distance(begin(), p), in distance(x.begin(), first), + //! and in distance(first, last). + //! + //! Note: Iterators of values obtained from list x now point to elements of this + //! list. Iterators of this list and all the references are not invalidated. + void splice(const_iterator p, BOOST_RV_REF(slist) x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { this->splice(p, static_cast(x), first, last); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const slist& x, const slist& y) + { + if(x.size() != y.size()){ + return false; + } + typedef typename slist::const_iterator const_iterator; + const_iterator end1 = x.end(); + + const_iterator i1 = x.begin(); + const_iterator i2 = y.begin(); + while (i1 != end1 && *i1 == *i2){ + ++i1; + ++i2; + } + return i1 == end1; + } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const slist& x, const slist& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const slist& x, const slist& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const slist& x, const slist& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const slist& x, const slist& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const slist& x, const slist& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(slist& x, slist& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + + void priv_push_front (const T &x) + { this->insert_after(this->cbefore_begin(), x); } + + void priv_push_front (BOOST_RV_REF(T) x) + { this->insert_after(this->cbefore_begin(), ::boost::move(x)); } + + bool priv_try_shrink(size_type new_size, const_iterator &last_pos) + { + typename Icont::iterator end_n(this->icont().end()), cur(this->icont().before_begin()), cur_next; + while (++(cur_next = cur) != end_n && new_size > 0){ + --new_size; + cur = cur_next; + } + last_pos = const_iterator(cur); + if (cur_next != end_n){ + this->erase_after(last_pos, const_iterator(end_n)); + return true; + } + else{ + return false; + } + } + + template + iterator priv_insert(const_iterator p, BOOST_FWD_REF(U) x) + { return this->insert_after(previous(p), ::boost::forward(x)); } + + template + iterator priv_insert_after(const_iterator prev_p, BOOST_FWD_REF(U) x) + { return iterator(this->icont().insert_after(prev_p.get(), *this->create_node(::boost::forward(x)))); } + + class insertion_functor; + friend class insertion_functor; + + class insertion_functor + { + Icont &icont_; + typedef typename Icont::iterator iiterator; + typedef typename Icont::const_iterator iconst_iterator; + const iconst_iterator prev_; + iiterator ret_; + + public: + insertion_functor(Icont &icont, typename Icont::const_iterator prev) + : icont_(icont), prev_(prev), ret_(prev.unconst()) + {} + + void operator()(Node &n) + { + ret_ = this->icont_.insert_after(prev_, n); + } + + iiterator inserted_first() const + { return ret_; } + }; + + //Functors for member algorithm defaults + struct value_less + { + bool operator()(const value_type &a, const value_type &b) const + { return a < b; } + }; + + struct value_equal + { + bool operator()(const value_type &a, const value_type &b) const + { return a == b; } + }; + + struct value_equal_to_this + { + explicit value_equal_to_this(const value_type &ref) + : m_ref(ref){} + + bool operator()(const value_type &val) const + { return m_ref == val; } + + const value_type &m_ref; + }; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +}} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public ::boost::has_trivial_destructor_after_move +{}; + +namespace container { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +}} //namespace boost{ namespace container { + +// Specialization of insert_iterator so that insertions will be constant +// time rather than linear time. + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//Ummm, I don't like to define things in namespace std, but +//there is no other way +namespace std { + +template +class insert_iterator > +{ + protected: + typedef boost::container::slist Container; + Container* container; + typename Container::iterator iter; + public: + typedef Container container_type; + typedef output_iterator_tag iterator_category; + typedef void value_type; + typedef void difference_type; + typedef void pointer; + typedef void reference; + + insert_iterator(Container& x, + typename Container::iterator i, + bool is_previous = false) + : container(&x), iter(is_previous ? i : x.previous(i)){ } + + insert_iterator& + operator=(const typename Container::value_type& value) + { + iter = container->insert_after(iter, value); + return *this; + } + insert_iterator& operator*(){ return *this; } + insert_iterator& operator++(){ return *this; } + insert_iterator& operator++(int){ return *this; } +}; + +} //namespace std; + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +#endif // BOOST_CONTAINER_SLIST_HPP diff --git a/boost/container/stable_vector.hpp b/boost/container/stable_vector.hpp new file mode 100644 index 0000000..b8b4157 --- /dev/null +++ b/boost/container/stable_vector.hpp @@ -0,0 +1,1949 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// +// Stable vector. +// +// Copyright 2008 Joaquin M Lopez Munoz. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_STABLE_VECTOR_HPP +#define BOOST_CONTAINER_STABLE_VECTOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +//#define STABLE_VECTOR_ENABLE_INVARIANT_CHECKING + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { +namespace container { + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace stable_vector_detail{ + +template +class clear_on_destroy +{ + public: + clear_on_destroy(C &c) + : c_(c), do_clear_(true) + {} + + void release() + { do_clear_ = false; } + + ~clear_on_destroy() + { + if(do_clear_){ + c_.clear(); + c_.priv_clear_pool(); + } + } + + private: + clear_on_destroy(const clear_on_destroy &); + clear_on_destroy &operator=(const clear_on_destroy &); + C &c_; + bool do_clear_; +}; + +template +struct node; + +template +struct node_base +{ + private: + typedef typename boost::intrusive:: + pointer_traits void_ptr_traits; + typedef typename void_ptr_traits:: + template rebind_pointer + ::type node_base_ptr; + typedef typename void_ptr_traits:: + template rebind_pointer + ::type node_base_ptr_ptr; + + public: + node_base(const node_base_ptr_ptr &n) + : up(n) + {} + + node_base() + : up() + {} + + node_base_ptr_ptr up; +}; + +template +struct node + : public node_base + ::template + rebind_pointer::type + > +{ + private: + node(); + + public: + typename ::boost::intrusive::pointer_traits::element_type value; +}; + +template +struct index_traits +{ + typedef boost::intrusive:: + pointer_traits + void_ptr_traits; + typedef stable_vector_detail:: + node_base node_base_type; + typedef typename void_ptr_traits::template + rebind_pointer::type node_base_ptr; + typedef typename void_ptr_traits::template + rebind_pointer::type node_base_ptr_ptr; + typedef boost::intrusive:: + pointer_traits node_base_ptr_traits; + typedef boost::intrusive:: + pointer_traits node_base_ptr_ptr_traits; + typedef typename allocator_traits:: + template portable_rebind_alloc + ::type node_base_ptr_allocator; + typedef ::boost::container::vector + index_type; + typedef typename index_type::iterator index_iterator; + typedef typename index_type::const_iterator const_index_iterator; + typedef typename index_type::size_type size_type; + + static const size_type ExtraPointers = 3; + //Stable vector stores metadata at the end of the index (node_base_ptr vector) with additional 3 pointers: + // back() is this->index.back() - ExtraPointers; + // end node index is *(this->index.end() - 3) + // Node cache first is *(this->index.end() - 2); + // Node cache last is this->index.back(); + + static node_base_ptr_ptr ptr_to_node_base_ptr(node_base_ptr &n) + { return node_base_ptr_ptr_traits::pointer_to(n); } + + static void fix_up_pointers(index_iterator first, index_iterator last) + { + while(first != last){ + typedef typename index_type::reference node_base_ptr_ref; + node_base_ptr_ref nbp = *first; + nbp->up = index_traits::ptr_to_node_base_ptr(nbp); + ++first; + } + } + + static index_iterator get_fix_up_end(index_type &index) + { return index.end() - (ExtraPointers - 1); } + + static void fix_up_pointers_from(index_type & index, index_iterator first) + { index_traits::fix_up_pointers(first, index_traits::get_fix_up_end(index)); } + + static void readjust_end_node(index_type &index, node_base_type &end_node) + { + if(!index.empty()){ + index_iterator end_node_it(index_traits::get_fix_up_end(index)); + node_base_ptr &end_node_idx_ref = *(--end_node_it); + end_node_idx_ref = node_base_ptr_traits::pointer_to(end_node); + end_node.up = node_base_ptr_ptr_traits::pointer_to(end_node_idx_ref); + } + else{ + end_node.up = node_base_ptr_ptr(); + } + } + + static void initialize_end_node(index_type &index, node_base_type &end_node, const size_type index_capacity_if_empty) + { + if(index.empty()){ + index.reserve(index_capacity_if_empty + ExtraPointers); + index.resize(ExtraPointers); + node_base_ptr &end_node_ref = *index.data(); + end_node_ref = node_base_ptr_traits::pointer_to(end_node); + end_node.up = index_traits::ptr_to_node_base_ptr(end_node_ref); + } + } + + #ifdef STABLE_VECTOR_ENABLE_INVARIANT_CHECKING + static bool invariants(index_type &index) + { + for( index_iterator it = index.begin() + , it_end = index_traits::get_fix_up_end(index) + ; it != it_end + ; ++it){ + if((*it)->up != index_traits::ptr_to_node_base_ptr(*it)){ + return false; + } + } + return true; + } + #endif //STABLE_VECTOR_ENABLE_INVARIANT_CHECKING +}; + +} //namespace stable_vector_detail + +template +class stable_vector_iterator +{ + typedef boost::intrusive::pointer_traits non_const_ptr_traits; + public: + typedef std::random_access_iterator_tag iterator_category; + typedef typename non_const_ptr_traits::element_type value_type; + typedef typename non_const_ptr_traits::difference_type difference_type; + typedef typename ::boost::container::container_detail::if_c + < IsConst + , typename non_const_ptr_traits::template + rebind_pointer::type + , Pointer + >::type pointer; + typedef boost::intrusive::pointer_traits ptr_traits; + typedef typename ptr_traits::reference reference; + + private: + typedef typename non_const_ptr_traits::template + rebind_pointer::type void_ptr; + typedef stable_vector_detail::node node_type; + typedef stable_vector_detail::node_base node_base_type; + typedef typename non_const_ptr_traits::template + rebind_pointer::type node_ptr; + typedef boost::intrusive:: + pointer_traits node_ptr_traits; + typedef typename non_const_ptr_traits::template + rebind_pointer::type node_base_ptr; + typedef typename non_const_ptr_traits::template + rebind_pointer::type node_base_ptr_ptr; + + node_base_ptr m_pn; + + public: + + explicit stable_vector_iterator(node_base_ptr p) BOOST_CONTAINER_NOEXCEPT + : m_pn(p) + {} + + stable_vector_iterator() BOOST_CONTAINER_NOEXCEPT + : m_pn() //Value initialization to achieve "null iterators" (N3644) + {} + + stable_vector_iterator(stable_vector_iterator const& other) BOOST_CONTAINER_NOEXCEPT + : m_pn(other.node_pointer()) + {} + + node_ptr node_pointer() const BOOST_CONTAINER_NOEXCEPT + { return node_ptr_traits::static_cast_from(m_pn); } + + public: + //Pointer like operators + reference operator*() const BOOST_CONTAINER_NOEXCEPT + { return node_pointer()->value; } + + pointer operator->() const BOOST_CONTAINER_NOEXCEPT + { return ptr_traits::pointer_to(this->operator*()); } + + //Increment / Decrement + stable_vector_iterator& operator++() BOOST_CONTAINER_NOEXCEPT + { + node_base_ptr_ptr p(this->m_pn->up); + this->m_pn = *(++p); + return *this; + } + + stable_vector_iterator operator++(int) BOOST_CONTAINER_NOEXCEPT + { stable_vector_iterator tmp(*this); ++*this; return stable_vector_iterator(tmp); } + + stable_vector_iterator& operator--() BOOST_CONTAINER_NOEXCEPT + { + node_base_ptr_ptr p(this->m_pn->up); + this->m_pn = *(--p); + return *this; + } + + stable_vector_iterator operator--(int) BOOST_CONTAINER_NOEXCEPT + { stable_vector_iterator tmp(*this); --*this; return stable_vector_iterator(tmp); } + + reference operator[](difference_type off) const BOOST_CONTAINER_NOEXCEPT + { return node_ptr_traits::static_cast_from(this->m_pn->up[off])->value; } + + stable_vector_iterator& operator+=(difference_type off) BOOST_CONTAINER_NOEXCEPT + { + if(off) this->m_pn = this->m_pn->up[off]; + return *this; + } + + friend stable_vector_iterator operator+(const stable_vector_iterator &left, difference_type off) BOOST_CONTAINER_NOEXCEPT + { + stable_vector_iterator tmp(left); + tmp += off; + return tmp; + } + + friend stable_vector_iterator operator+(difference_type off, const stable_vector_iterator& right) BOOST_CONTAINER_NOEXCEPT + { + stable_vector_iterator tmp(right); + tmp += off; + return tmp; + } + + stable_vector_iterator& operator-=(difference_type off) BOOST_CONTAINER_NOEXCEPT + { *this += -off; return *this; } + + friend stable_vector_iterator operator-(const stable_vector_iterator &left, difference_type off) BOOST_CONTAINER_NOEXCEPT + { + stable_vector_iterator tmp(left); + tmp -= off; + return tmp; + } + + friend difference_type operator-(const stable_vector_iterator& left, const stable_vector_iterator& right) BOOST_CONTAINER_NOEXCEPT + { return left.m_pn->up - right.m_pn->up; } + + //Comparison operators + friend bool operator== (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn == r.m_pn; } + + friend bool operator!= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn != r.m_pn; } + + friend bool operator< (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn->up < r.m_pn->up; } + + friend bool operator<= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn->up <= r.m_pn->up; } + + friend bool operator> (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn->up > r.m_pn->up; } + + friend bool operator>= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_pn->up >= r.m_pn->up; } +}; + + #if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING) + + #define STABLE_VECTOR_CHECK_INVARIANT \ + invariant_checker BOOST_JOIN(check_invariant_,__LINE__)(*this); \ + BOOST_JOIN(check_invariant_,__LINE__).touch(); + + #else //STABLE_VECTOR_ENABLE_INVARIANT_CHECKING + + #define STABLE_VECTOR_CHECK_INVARIANT + + #endif //#if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING) + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! Originally developed by Joaquin M. Lopez Munoz, stable_vector is a std::vector +//! drop-in replacement implemented as a node container, offering iterator and reference +//! stability. +//! +//! Here are the details taken from the author's blog +//! ( +//! Introducing stable_vector): +//! +//! We present stable_vector, a fully STL-compliant stable container that provides +//! most of the features of std::vector except element contiguity. +//! +//! General properties: stable_vector satisfies all the requirements of a container, +//! a reversible container and a sequence and provides all the optional operations +//! present in std::vector. Like std::vector, iterators are random access. +//! stable_vector does not provide element contiguity; in exchange for this absence, +//! the container is stable, i.e. references and iterators to an element of a stable_vector +//! remain valid as long as the element is not erased, and an iterator that has been +//! assigned the return value of end() always remain valid until the destruction of +//! the associated stable_vector. +//! +//! Operation complexity: The big-O complexities of stable_vector operations match +//! exactly those of std::vector. In general, insertion/deletion is constant time at +//! the end of the sequence and linear elsewhere. Unlike std::vector, stable_vector +//! does not internally perform any value_type destruction, copy or assignment +//! operations other than those exactly corresponding to the insertion of new +//! elements or deletion of stored elements, which can sometimes compensate in terms +//! of performance for the extra burden of doing more pointer manipulation and an +//! additional allocation per element. +//! +//! Exception safety: As stable_vector does not internally copy elements around, some +//! operations provide stronger exception safety guarantees than in std::vector. +//! +//! \tparam T The type of object that is stored in the stable_vector +//! \tparam Allocator The allocator used for all internal memory management +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template > +#else +template +#endif +class stable_vector +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef allocator_traits allocator_traits_type; + typedef boost::intrusive:: + pointer_traits + ptr_traits; + typedef typename ptr_traits:: + template rebind_pointer::type void_ptr; + typedef typename allocator_traits_type:: + template portable_rebind_alloc + ::type void_allocator_type; + typedef stable_vector_detail::index_traits + index_traits_type; + typedef typename index_traits_type::node_base_type node_base_type; + typedef typename index_traits_type::node_base_ptr node_base_ptr; + typedef typename index_traits_type:: + node_base_ptr_ptr node_base_ptr_ptr; + typedef typename index_traits_type:: + node_base_ptr_traits node_base_ptr_traits; + typedef typename index_traits_type:: + node_base_ptr_ptr_traits node_base_ptr_ptr_traits; + typedef typename index_traits_type::index_type index_type; + typedef typename index_traits_type::index_iterator index_iterator; + typedef typename index_traits_type:: + const_index_iterator const_index_iterator; + typedef stable_vector_detail::node + node_type; + typedef typename ptr_traits::template + rebind_pointer::type node_ptr; + typedef boost::intrusive:: + pointer_traits node_ptr_traits; + typedef typename ptr_traits::template + rebind_pointer::type const_node_ptr; + typedef boost::intrusive:: + pointer_traits const_node_ptr_traits; + typedef typename node_ptr_traits::reference node_reference; + typedef typename const_node_ptr_traits::reference const_node_reference; + + typedef ::boost::container::container_detail:: + integral_constant allocator_v1; + typedef ::boost::container::container_detail:: + integral_constant allocator_v2; + typedef ::boost::container::container_detail::integral_constant + ::value> alloc_version; + typedef typename allocator_traits_type:: + template portable_rebind_alloc + ::type node_allocator_type; + + typedef ::boost::container::container_detail:: + allocator_version_traits allocator_version_traits_t; + typedef typename allocator_version_traits_t::multiallocation_chain multiallocation_chain; + + node_ptr allocate_one() + { return allocator_version_traits_t::allocate_one(this->priv_node_alloc()); } + + void deallocate_one(const node_ptr &p) + { allocator_version_traits_t::deallocate_one(this->priv_node_alloc(), p); } + + void allocate_individual(typename allocator_traits_type::size_type n, multiallocation_chain &m) + { allocator_version_traits_t::allocate_individual(this->priv_node_alloc(), n, m); } + + void deallocate_individual(multiallocation_chain &holder) + { allocator_version_traits_t::deallocate_individual(this->priv_node_alloc(), holder); } + + friend class stable_vector_detail::clear_on_destroy; + typedef stable_vector_iterator + < typename allocator_traits::pointer + , false> iterator_impl; + typedef stable_vector_iterator + < typename allocator_traits::pointer + , false> const_iterator_impl; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + public: + + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef T value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef node_allocator_type stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) const_reverse_iterator; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(stable_vector) + static const size_type ExtraPointers = index_traits_type::ExtraPointers; + + class insert_rollback; + friend class insert_rollback; + + class push_back_rollback; + friend class push_back_rollback; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Default constructs a stable_vector. + //! + //! Throws: If allocator_type's default constructor throws. + //! + //! Complexity: Constant. + stable_vector() + : internal_data(), index() + { + STABLE_VECTOR_CHECK_INVARIANT; + } + + //! Effects: Constructs a stable_vector taking the allocator as parameter. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + explicit stable_vector(const allocator_type& al) BOOST_CONTAINER_NOEXCEPT + : internal_data(al), index(al) + { + STABLE_VECTOR_CHECK_INVARIANT; + } + + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts n value initialized values. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + explicit stable_vector(size_type n) + : internal_data(), index() + { + stable_vector_detail::clear_on_destroy cod(*this); + this->resize(n); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts n default initialized values. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + //! + //! Note: Non-standard extension + stable_vector(size_type n, default_init_t) + : internal_data(), index() + { + stable_vector_detail::clear_on_destroy cod(*this); + this->resize(n, default_init); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's default or copy constructor throws. + //! + //! Complexity: Linear to n. + stable_vector(size_type n, const T& t, const allocator_type& al = allocator_type()) + : internal_data(al), index(al) + { + stable_vector_detail::clear_on_destroy cod(*this); + this->insert(this->cend(), n, t); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts a copy of the range [first, last) in the stable_vector. + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + stable_vector(InputIterator first,InputIterator last, const allocator_type& al = allocator_type()) + : internal_data(al), index(al) + { + stable_vector_detail::clear_on_destroy cod(*this); + this->insert(this->cend(), first, last); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + + //! Effects: Copy constructs a stable_vector. + //! + //! Postcondition: x == *this. + //! + //! Complexity: Linear to the elements x contains. + stable_vector(const stable_vector& x) + : internal_data(allocator_traits:: + select_on_container_copy_construction(x.priv_node_alloc())) + , index(allocator_traits:: + select_on_container_copy_construction(x.index.get_stored_allocator())) + { + stable_vector_detail::clear_on_destroy cod(*this); + this->insert(this->cend(), x.begin(), x.end()); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.last()) in the stable_vector + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + stable_vector(std::initializer_list il, const allocator_type& l = allocator_type()) + : internal_data(l), index(l) + { + stable_vector_detail::clear_on_destroy cod(*this); + insert(cend(), il.begin(), il.end()) + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } +#endif + + //! Effects: Move constructor. Moves mx's resources to *this. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant. + stable_vector(BOOST_RV_REF(stable_vector) x) + : internal_data(boost::move(x.priv_node_alloc())), index(boost::move(x.index)) + { + this->priv_swap_members(x); + } + + //! Effects: Copy constructs a stable_vector using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Complexity: Linear to the elements x contains. + stable_vector(const stable_vector& x, const allocator_type &a) + : internal_data(a), index(a) + { + stable_vector_detail::clear_on_destroy cod(*this); + this->insert(this->cend(), x.begin(), x.end()); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + + //! Effects: Move constructor using the specified allocator. + //! Moves mx's resources to *this. + //! + //! Throws: If allocator_type's copy constructor throws. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise + stable_vector(BOOST_RV_REF(stable_vector) x, const allocator_type &a) + : internal_data(a), index(a) + { + if(this->priv_node_alloc() == x.priv_node_alloc()){ + this->priv_swap_members(x); + } + else{ + stable_vector_detail::clear_on_destroy cod(*this); + this->insert(this->cend(), x.begin(), x.end()); + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } + } + + //! Effects: Destroys the stable_vector. All stored values are destroyed + //! and used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements. + ~stable_vector() + { + this->clear(); + this->priv_clear_pool(); + } + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to the number of elements in x. + stable_vector& operator=(BOOST_COPY_ASSIGN_REF(stable_vector) x) + { + STABLE_VECTOR_CHECK_INVARIANT; + if (&x != this){ + node_allocator_type &this_alloc = this->priv_node_alloc(); + const node_allocator_type &x_alloc = x.priv_node_alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + this->shrink_to_fit(); + } + container_detail::assign_alloc(this->priv_node_alloc(), x.priv_node_alloc(), flag); + container_detail::assign_alloc(this->index.get_stored_allocator(), x.index.get_stored_allocator(), flag); + this->assign(x.begin(), x.end()); + } + return *this; + } + + //! Effects: Move assignment. All mx's values are transferred to *this. + //! + //! Postcondition: x.empty(). *this contains a the elements x had + //! before the function. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or T's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + stable_vector& operator=(BOOST_RV_REF(stable_vector) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + //for move constructor, no aliasing (&x != this) is assummed. + BOOST_ASSERT(this != &x); + node_allocator_type &this_alloc = this->priv_node_alloc(); + node_allocator_type &x_alloc = x.priv_node_alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + container_detail::bool_ flag; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy objects but retain memory in case x reuses it in the future + this->clear(); + //Move allocator if needed + container_detail::move_alloc(this_alloc, x_alloc, flag); + //Take resources + this->index = boost::move(x.index); + this->priv_swap_members(x); + } + //Else do a one by one move + else{ + this->assign( boost::make_move_iterator(x.begin()) + , boost::make_move_iterator(x.end())); + } + return *this; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Make *this container contains elements from il. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + stable_vector& operator=(std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + assign(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Assigns the n copies of val to *this. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + void assign(size_type n, const T& t) + { + typedef constant_iterator cvalue_iterator; + this->assign(cvalue_iterator(t, n), cvalue_iterator()); + } + + //! Effects: Assigns the the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing InpIt throws. + //! + //! Complexity: Linear to n. + template + void assign(InputIterator first,InputIterator last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + >::type * = 0 + #endif + ) + { + STABLE_VECTOR_CHECK_INVARIANT; + iterator first1 = this->begin(); + iterator last1 = this->end(); + for ( ; first1 != last1 && first != last; ++first1, ++first) + *first1 = *first; + if (first == last){ + this->erase(first1, last1); + } + else{ + this->insert(last1, first, last); + } + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing initializer_list iterator throws. + //! + void assign(std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + assign(il.begin(), il.end()); + } +#endif + + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const + { return this->priv_node_alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_node_alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return this->priv_node_alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return (this->index.empty()) ? this->end(): iterator(node_ptr_traits::static_cast_from(this->index.front())); } + + //! Effects: Returns a const_iterator to the first element contained in the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return (this->index.empty()) ? this->cend() : const_iterator(node_ptr_traits::static_cast_from(this->index.front())) ; } + + //! Effects: Returns an iterator to the end of the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->priv_get_end_node()); } + + //! Effects: Returns a const_iterator to the end of the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->priv_get_end_node()); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->end()); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->begin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->begin()); } + + //! Effects: Returns a const_iterator to the first element contained in the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->begin(); } + + //! Effects: Returns a const_iterator to the end of the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return this->end(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->rbegin(); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend()const BOOST_CONTAINER_NOEXCEPT + { return this->rend(); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the stable_vector contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return this->index.size() <= ExtraPointers; } + + //! Effects: Returns the number of the elements contained in the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { + const size_type index_size = this->index.size(); + return (index_size - ExtraPointers) & (std::size_t(0u) -std::size_t(index_size != 0)); + } + + //! Effects: Returns the largest possible size of the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return this->index.max_size() - ExtraPointers; } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws, or T's value initialization throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type n) + { + typedef value_init_construct_iterator value_init_iterator; + STABLE_VECTOR_CHECK_INVARIANT; + if(n > this->size()) + this->insert(this->cend(), value_init_iterator(n - this->size()), value_init_iterator()); + else if(n < this->size()) + this->erase(this->cbegin() + n, this->cend()); + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are default initialized. + //! + //! Throws: If memory allocation throws, or T's default initialization throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + //! + //! Note: Non-standard extension + void resize(size_type n, default_init_t) + { + typedef default_init_construct_iterator default_init_iterator; + STABLE_VECTOR_CHECK_INVARIANT; + if(n > this->size()) + this->insert(this->cend(), default_init_iterator(n - this->size()), default_init_iterator()); + else if(n < this->size()) + this->erase(this->cbegin() + n, this->cend()); + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws, or T's copy constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type n, const T& t) + { + STABLE_VECTOR_CHECK_INVARIANT; + if(n > this->size()) + this->insert(this->cend(), n - this->size(), t); + else if(n < this->size()) + this->erase(this->cbegin() + n, this->cend()); + } + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT + { + const size_type index_size = this->index.size(); + BOOST_ASSERT(!index_size || index_size >= ExtraPointers); + const size_type bucket_extra_capacity = this->index.capacity()- index_size; + const size_type node_extra_capacity = this->internal_data.pool_size; + const size_type extra_capacity = (bucket_extra_capacity < node_extra_capacity) + ? bucket_extra_capacity : node_extra_capacity; + const size_type index_offset = + (ExtraPointers + extra_capacity) & (size_type(0u) - size_type(index_size != 0)); + return index_size - index_offset; + } + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws. + void reserve(size_type n) + { + STABLE_VECTOR_CHECK_INVARIANT; + if(n > this->max_size()){ + throw_length_error("stable_vector::reserve max_size() exceeded"); + } + + size_type sz = this->size(); + size_type old_capacity = this->capacity(); + if(n > old_capacity){ + index_traits_type::initialize_end_node(this->index, this->internal_data.end_node, n); + const void * old_ptr = &index[0]; + this->index.reserve(n + ExtraPointers); + bool realloced = &index[0] != old_ptr; + //Fix the pointers for the newly allocated buffer + if(realloced){ + index_traits_type::fix_up_pointers_from(this->index, this->index.begin()); + } + //Now fill pool if data is not enough + if((n - sz) > this->internal_data.pool_size){ + this->priv_increase_pool((n - sz) - this->internal_data.pool_size); + } + } + } + + //! Effects: Tries to deallocate the excess of memory created + //! with previous allocations. The size of the stable_vector is unchanged + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { + if(this->capacity()){ + //First empty allocated node pool + this->priv_clear_pool(); + //If empty completely destroy the index, let's recover default-constructed state + if(this->empty()){ + this->index.clear(); + this->index.shrink_to_fit(); + this->internal_data.end_node.up = node_base_ptr_ptr(); + } + //Otherwise, try to shrink-to-fit the index and readjust pointers if necessary + else{ + const void* old_ptr = &index[0]; + this->index.shrink_to_fit(); + bool realloced = &index[0] != old_ptr; + //Fix the pointers for the newly allocated buffer + if(realloced){ + index_traits_type::fix_up_pointers_from(this->index, this->index.begin()); + } + } + } + } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this->index.front()).value; } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this->index.front()).value; } + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference back() BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this->index[this->size()-1u]).value; } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference back() const BOOST_CONTAINER_NOEXCEPT + { return static_cast(*this->index[this->size()-1u]).value; } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(n < this->size()); + return static_cast(*this->index[n]).value; + } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(n < this->size()); + return static_cast(*this->index[n]).value; + } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + reference at(size_type n) + { + if(n >= this->size()){ + throw_out_of_range("vector::at invalid subscript"); + } + return operator[](n); + } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + const_reference at(size_type n)const + { + if(n >= this->size()){ + throw_out_of_range("vector::at invalid subscript"); + } + return operator[](n); + } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the end of the stable_vector. + //! + //! Throws: If memory allocation throws or the in-place constructor throws. + //! + //! Complexity: Amortized constant time. + template + void emplace_back(Args &&...args) + { + typedef emplace_functor EmplaceFunctor; + typedef emplace_iterator EmplaceIterator; + EmplaceFunctor &&ef = EmplaceFunctor(boost::forward(args)...); + this->insert(this->cend(), EmplaceIterator(ef), EmplaceIterator()); + } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... before p + //! + //! Throws: If memory allocation throws or the in-place constructor throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + template + iterator emplace(const_iterator p, Args && ...args) + { + size_type pos_n = p - cbegin(); + typedef emplace_functor EmplaceFunctor; + typedef emplace_iterator EmplaceIterator; + EmplaceFunctor &&ef = EmplaceFunctor(boost::forward(args)...); + this->insert(p, EmplaceIterator(ef), EmplaceIterator()); + return iterator(this->begin() + pos_n); + } + + #else + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + typedef BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \ + BOOST_PP_EXPR_IF(n, <) BOOST_PP_ENUM_PARAMS(n, P) BOOST_PP_EXPR_IF(n, >) \ + EmplaceFunctor; \ + typedef emplace_iterator EmplaceIterator; \ + EmplaceFunctor ef BOOST_PP_LPAREN_IF(n) \ + BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) \ + BOOST_PP_RPAREN_IF(n); \ + this->insert(this->cend() , EmplaceIterator(ef), EmplaceIterator()); \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(const_iterator p \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + typedef BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \ + BOOST_PP_EXPR_IF(n, <) BOOST_PP_ENUM_PARAMS(n, P) BOOST_PP_EXPR_IF(n, >) \ + EmplaceFunctor; \ + typedef emplace_iterator EmplaceIterator; \ + EmplaceFunctor ef BOOST_PP_LPAREN_IF(n) \ + BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) \ + BOOST_PP_RPAREN_IF(n); \ + size_type pos_n = p - this->cbegin(); \ + this->insert(p, EmplaceIterator(ef), EmplaceIterator()); \ + return iterator(this->begin() + pos_n); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the end of the stable_vector. + //! + //! Throws: If memory allocation throws or + //! T's copy constructor throws. + //! + //! Complexity: Amortized constant time. + void push_back(const T &x); + + //! Effects: Constructs a new element in the end of the stable_vector + //! and moves the resources of mx to this new element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: Amortized constant time. + void push_back(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of x before p. + //! + //! Returns: An iterator to the inserted element. + //! + //! Throws: If memory allocation throws or x's copy constructor throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator p, const T &x); + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a new element before p with mx's resources. + //! + //! Returns: an iterator to the inserted element. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: If p is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator p, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert n copies of x before p. + //! + //! Returns: an iterator to the first inserted element or p if n is 0. + //! + //! Throws: If memory allocation throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + iterator insert(const_iterator p, size_type n, const T& t) + { + STABLE_VECTOR_CHECK_INVARIANT; + typedef constant_iterator cvalue_iterator; + return this->insert(p, cvalue_iterator(t, n), cvalue_iterator()); + } + + //! Requires: p must be a valid iterator of *this. +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before p. + //! + //! Returns: an iterator to the first inserted element or p if first == last. + //! + //! Complexity: Linear to std::distance [il.begin(), il.end()). + iterator insert(const_iterator p, std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + return insert(p, il.begin(), il.end()); + } +#endif + + //! Requires: pos must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [first, last) range before p. + //! + //! Returns: an iterator to the first inserted element or p if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws or T's copy constructor throws. + //! + //! Complexity: Linear to std::distance [first, last). + template + iterator insert(const_iterator p, InputIterator first, InputIterator last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + STABLE_VECTOR_CHECK_INVARIANT; + const size_type pos_n = p - this->cbegin(); + for(; first != last; ++first){ + this->emplace(p, *first); + } + return this->begin() + pos_n; + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator p, FwdIt first, FwdIt last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + ) + { + const size_type num_new = static_cast(std::distance(first, last)); + const size_type idx = static_cast(p - this->cbegin()); + if(num_new){ + //Fills the node pool and inserts num_new null pointers in idx. + //If a new buffer was needed fixes up pointers up to idx so + //past-new nodes are not aligned until the end of this function + //or in a rollback in case of exception + index_iterator it_past_newly_constructed(this->priv_insert_forward_non_templated(idx, num_new)); + const index_iterator it_past_new(it_past_newly_constructed + num_new); + { + //Prepare rollback + insert_rollback rollback(*this, it_past_newly_constructed, it_past_new); + while(first != last){ + const node_ptr p = this->priv_get_from_pool(); + BOOST_ASSERT(!!p); + //Put it in the index so rollback can return it in pool if construct_in_place throws + *it_past_newly_constructed = p; + //Constructs and fixes up pointers This can throw + this->priv_build_node_from_it(p, it_past_newly_constructed, first); + ++first; + ++it_past_newly_constructed; + } + //rollback.~insert_rollback() called in case of exception + } + //Fix up pointers for past-new nodes (new nodes were fixed during construction) and + //nodes before insertion p in priv_insert_forward_non_templated(...) + index_traits_type::fix_up_pointers_from(this->index, it_past_newly_constructed); + } + return this->begin() + idx; + } + #endif + + //! Effects: Removes the last element from the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + void pop_back() BOOST_CONTAINER_NOEXCEPT + { this->erase(--this->cend()); } + + //! Effects: Erases the element at p. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the elements between p and the + //! last element. Constant if p is the last element. + iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT + { + STABLE_VECTOR_CHECK_INVARIANT; + const size_type d = p - this->cbegin(); + index_iterator it = this->index.begin() + d; + this->priv_delete_node(p.node_pointer()); + it = this->index.erase(it); + index_traits_type::fix_up_pointers_from(this->index, it); + return iterator(node_ptr_traits::static_cast_from(*it)); + } + + //! Effects: Erases the elements pointed by [first, last). + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the distance between first and last + //! plus linear to the elements between p and the last element. + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { + STABLE_VECTOR_CHECK_INVARIANT; + const const_iterator cbeg(this->cbegin()); + const size_type d1 = static_cast(first - cbeg), + d2 = static_cast(last - cbeg); + size_type d_dif = d2 - d1; + if(d_dif){ + multiallocation_chain holder; + const index_iterator it1(this->index.begin() + d1); + const index_iterator it2(it1 + d_dif); + index_iterator it(it1); + while(d_dif--){ + node_base_ptr &nb = *it; + ++it; + node_type &n = *node_ptr_traits::static_cast_from(nb); + this->priv_destroy_node(n); + holder.push_back(node_ptr_traits::pointer_to(n)); + } + this->priv_put_in_pool(holder); + const index_iterator e = this->index.erase(it1, it2); + index_traits_type::fix_up_pointers_from(this->index, e); + } + return iterator(last.node_pointer()); + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(stable_vector & x) + { + STABLE_VECTOR_CHECK_INVARIANT; + container_detail::bool_ flag; + container_detail::swap_alloc(this->priv_node_alloc(), x.priv_node_alloc(), flag); + //vector's allocator is swapped here + this->index.swap(x.index); + this->priv_swap_members(x); + } + + //! Effects: Erases all the elements of the stable_vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the stable_vector. + void clear() BOOST_CONTAINER_NOEXCEPT + { this->erase(this->cbegin(),this->cend()); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const stable_vector& x, const stable_vector& y) + { return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin()); } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const stable_vector& x, const stable_vector& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const stable_vector& x, const stable_vector& y) + { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const stable_vector& x, const stable_vector& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const stable_vector& x, const stable_vector& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const stable_vector& x, const stable_vector& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(stable_vector& x, stable_vector& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + + class insert_rollback + { + public: + + insert_rollback(stable_vector &sv, index_iterator &it_past_constructed, const index_iterator &it_past_new) + : m_sv(sv), m_it_past_constructed(it_past_constructed), m_it_past_new(it_past_new) + {} + + ~insert_rollback() + { + if(m_it_past_constructed != m_it_past_new){ + m_sv.priv_put_in_pool(node_ptr_traits::static_cast_from(*m_it_past_constructed)); + index_iterator e = m_sv.index.erase(m_it_past_constructed, m_it_past_new); + index_traits_type::fix_up_pointers_from(m_sv.index, e); + } + } + + private: + stable_vector &m_sv; + index_iterator &m_it_past_constructed; + const index_iterator &m_it_past_new; + }; + + class push_back_rollback + { + public: + push_back_rollback(stable_vector &sv, const node_ptr &p) + : m_sv(sv), m_p(p) + {} + + ~push_back_rollback() + { + if(m_p){ + m_sv.priv_put_in_pool(m_p); + } + } + + void release() + { m_p = node_ptr(); } + + private: + stable_vector &m_sv; + node_ptr m_p; + }; + + index_iterator priv_insert_forward_non_templated(size_type idx, size_type num_new) + { + index_traits_type::initialize_end_node(this->index, this->internal_data.end_node, num_new); + + //Now try to fill the pool with new data + if(this->internal_data.pool_size < num_new){ + this->priv_increase_pool(num_new - this->internal_data.pool_size); + } + + //Now try to make room in the vector + const node_base_ptr_ptr old_buffer = this->index.data(); + this->index.insert(this->index.begin() + idx, num_new, node_ptr()); + bool new_buffer = this->index.data() != old_buffer; + + //Fix the pointers for the newly allocated buffer + const index_iterator index_beg = this->index.begin(); + if(new_buffer){ + index_traits_type::fix_up_pointers(index_beg, index_beg + idx); + } + return index_beg + idx; + } + + bool priv_capacity_bigger_than_size() const + { + return this->index.capacity() > this->index.size() && + this->internal_data.pool_size > 0; + } + + template + void priv_push_back(BOOST_MOVE_CATCH_FWD(U) x) + { + if(this->priv_capacity_bigger_than_size()){ + //Enough memory in the pool and in the index + const node_ptr p = this->priv_get_from_pool(); + BOOST_ASSERT(!!p); + { + push_back_rollback rollback(*this, p); + //This might throw + this->priv_build_node_from_convertible(p, ::boost::forward(x)); + rollback.release(); + } + //This can't throw as there is room for a new elements in the index + index_iterator new_index = this->index.insert(this->index.end() - ExtraPointers, p); + index_traits_type::fix_up_pointers_from(this->index, new_index); + } + else{ + this->insert(this->cend(), ::boost::forward(x)); + } + } + + iterator priv_insert(const_iterator p, const value_type &t) + { + typedef constant_iterator cvalue_iterator; + return this->insert(p, cvalue_iterator(t, 1), cvalue_iterator()); + } + + iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x) + { + typedef repeat_iterator repeat_it; + typedef boost::move_iterator repeat_move_it; + //Just call more general insert(p, size, value) and return iterator + return this->insert(p, repeat_move_it(repeat_it(x, 1)), repeat_move_it(repeat_it())); + } + + void priv_clear_pool() + { + if(!this->index.empty() && this->index.back()){ + node_base_ptr &pool_first_ref = *(this->index.end() - 2); + node_base_ptr &pool_last_ref = this->index.back(); + + multiallocation_chain holder; + holder.incorporate_after( holder.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + this->deallocate_individual(holder); + pool_first_ref = pool_last_ref = 0; + this->internal_data.pool_size = 0; + } + } + + void priv_increase_pool(size_type n) + { + node_base_ptr &pool_first_ref = *(this->index.end() - 2); + node_base_ptr &pool_last_ref = this->index.back(); + multiallocation_chain holder; + holder.incorporate_after( holder.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + multiallocation_chain m; + this->allocate_individual(n, m); + holder.splice_after(holder.before_begin(), m, m.before_begin(), m.last(), n); + this->internal_data.pool_size += n; + std::pair data(holder.extract_data()); + pool_first_ref = data.first; + pool_last_ref = data.second; + } + + void priv_put_in_pool(const node_ptr &p) + { + node_base_ptr &pool_first_ref = *(this->index.end()-2); + node_base_ptr &pool_last_ref = this->index.back(); + multiallocation_chain holder; + holder.incorporate_after( holder.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + holder.push_front(p); + ++this->internal_data.pool_size; + std::pair ret(holder.extract_data()); + pool_first_ref = ret.first; + pool_last_ref = ret.second; + } + + void priv_put_in_pool(multiallocation_chain &ch) + { + node_base_ptr &pool_first_ref = *(this->index.end()-(ExtraPointers-1)); + node_base_ptr &pool_last_ref = this->index.back(); + ch.incorporate_after( ch.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + this->internal_data.pool_size = ch.size(); + const std::pair ret(ch.extract_data()); + pool_first_ref = ret.first; + pool_last_ref = ret.second; + } + + node_ptr priv_get_from_pool() + { + //Precondition: index is not empty + BOOST_ASSERT(!this->index.empty()); + node_base_ptr &pool_first_ref = *(this->index.end() - (ExtraPointers-1)); + node_base_ptr &pool_last_ref = this->index.back(); + multiallocation_chain holder; + holder.incorporate_after( holder.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + node_ptr ret = holder.pop_front(); + --this->internal_data.pool_size; + if(!internal_data.pool_size){ + pool_first_ref = pool_last_ref = node_ptr(); + } + else{ + const std::pair data(holder.extract_data()); + pool_first_ref = data.first; + pool_last_ref = data.second; + } + return ret; + } + + node_base_ptr priv_get_end_node() const + { return node_base_ptr_traits::pointer_to(const_cast(this->internal_data.end_node)); } + + void priv_destroy_node(const node_type &n) + { + allocator_traits:: + destroy(this->priv_node_alloc(), container_detail::addressof(n.value)); + static_cast(&n)->~node_base_type(); + } + + void priv_delete_node(const node_ptr &n) + { + this->priv_destroy_node(*n); + this->priv_put_in_pool(n); + } + + template + void priv_build_node_from_it(const node_ptr &p, const index_iterator &up_index, const Iterator &it) + { + //This can throw + boost::container::construct_in_place + ( this->priv_node_alloc() + , container_detail::addressof(p->value) + , it); + //This does not throw + ::new(static_cast(container_detail::to_raw_pointer(p)), boost_container_new_t()) + node_base_type(index_traits_type::ptr_to_node_base_ptr(*up_index)); + } + + template + void priv_build_node_from_convertible(const node_ptr &p, BOOST_FWD_REF(ValueConvertible) value_convertible) + { + //This can throw + boost::container::allocator_traits::construct + ( this->priv_node_alloc() + , container_detail::addressof(p->value) + , ::boost::forward(value_convertible)); + //This does not throw + ::new(static_cast(container_detail::to_raw_pointer(p)), boost_container_new_t()) node_base_type; + } + + void priv_swap_members(stable_vector &x) + { + boost::container::swap_dispatch(this->internal_data.pool_size, x.internal_data.pool_size); + index_traits_type::readjust_end_node(this->index, this->internal_data.end_node); + index_traits_type::readjust_end_node(x.index, x.internal_data.end_node); + } + + #if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING) + bool priv_invariant()const + { + index_type & index_ref = const_cast(this->index); + + if(index.empty()) + return !this->capacity() && !this->size(); + if(this->priv_get_end_node() != *(index.end() - ExtraPointers)){ + return false; + } + + if(!index_traits_type::invariants(index_ref)){ + return false; + } + + size_type n = this->capacity() - this->size(); + node_base_ptr &pool_first_ref = *(index_ref.end() - (ExtraPointers-1)); + node_base_ptr &pool_last_ref = index_ref.back(); + multiallocation_chain holder; + holder.incorporate_after( holder.before_begin() + , node_ptr_traits::static_cast_from(pool_first_ref) + , node_ptr_traits::static_cast_from(pool_last_ref) + , internal_data.pool_size); + typename multiallocation_chain::iterator beg(holder.begin()), end(holder.end()); + size_type num_pool = 0; + while(beg != end){ + ++num_pool; + ++beg; + } + return n >= num_pool && num_pool == internal_data.pool_size; + } + + class invariant_checker + { + invariant_checker(const invariant_checker &); + invariant_checker & operator=(const invariant_checker &); + const stable_vector* p; + + public: + invariant_checker(const stable_vector& v):p(&v){} + ~invariant_checker(){BOOST_ASSERT(p->priv_invariant());} + void touch(){} + }; + #endif + + class ebo_holder + : public node_allocator_type + { + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(ebo_holder) + + public: + template + explicit ebo_holder(BOOST_FWD_REF(AllocatorRLValue) a) + : node_allocator_type(boost::forward(a)) + , pool_size(0) + , end_node() + {} + + ebo_holder() + : node_allocator_type() + , pool_size(0) + , end_node() + {} + + size_type pool_size; + node_base_type end_node; + } internal_data; + + node_allocator_type &priv_node_alloc() { return internal_data; } + const node_allocator_type &priv_node_alloc() const { return internal_data; } + + index_type index; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#undef STABLE_VECTOR_CHECK_INVARIANT + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +/* + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public has_trivial_destructor_after_move::value +{}; + +*/ + +}} + +#include + +#endif //BOOST_CONTAINER_STABLE_VECTOR_HPP diff --git a/boost/container/static_vector.hpp b/boost/container/static_vector.hpp new file mode 100644 index 0000000..1ae8011 --- /dev/null +++ b/boost/container/static_vector.hpp @@ -0,0 +1,1156 @@ +// Boost.Container static_vector +// +// Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland. +// Copyright (c) 2011-2013 Andrew Hundt. +// Copyright (c) 2013-2014 Ion Gaztanaga +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_CONTAINER_STATIC_VECTOR_HPP +#define BOOST_CONTAINER_STATIC_VECTOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +#include +#include + +namespace boost { namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace container_detail { + +template +class static_storage_allocator +{ + public: + typedef T value_type; + + static_storage_allocator() BOOST_CONTAINER_NOEXCEPT + {} + + static_storage_allocator(const static_storage_allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + static_storage_allocator & operator=(const static_storage_allocator &) BOOST_CONTAINER_NOEXCEPT + {} + + T* internal_storage() const BOOST_CONTAINER_NOEXCEPT + { return const_cast(static_cast(static_cast(&storage))); } + + T* internal_storage() BOOST_CONTAINER_NOEXCEPT + { return static_cast(static_cast(&storage)); } + + static const std::size_t internal_capacity = N; + + typedef boost::container::container_detail::version_type version; + + friend bool operator==(const static_storage_allocator &, const static_storage_allocator &) BOOST_CONTAINER_NOEXCEPT + { return false; } + + friend bool operator!=(const static_storage_allocator &, const static_storage_allocator &) BOOST_CONTAINER_NOEXCEPT + { return true; } + + private: + typename boost::aligned_storage + ::value>::type storage; +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! +//!@brief A variable-size array container with fixed capacity. +//! +//!static_vector is a sequence container like boost::container::vector with contiguous storage that can +//!change in size, along with the static allocation, low overhead, and fixed capacity of boost::array. +//! +//!A static_vector is a sequence that supports random access to elements, constant time insertion and +//!removal of elements at the end, and linear time insertion and removal of elements at the beginning or +//!in the middle. The number of elements in a static_vector may vary dynamically up to a fixed capacity +//!because elements are stored within the object itself similarly to an array. However, objects are +//!initialized as they are inserted into static_vector unlike C arrays or std::array which must construct +//!all elements on instantiation. The behavior of static_vector enables the use of statically allocated +//!elements in cases with complex object lifetime requirements that would otherwise not be trivially +//!possible. +//! +//!@par Error Handling +//! Insertion beyond the capacity result in throwing std::bad_alloc() if exceptions are enabled or +//! calling throw_bad_alloc() if not enabled. +//! +//! std::out_of_range is thrown if out of bound access is performed in at() if exceptions are +//! enabled, throw_out_of_range() if not enabled. +//! +//!@tparam Value The type of element that will be stored. +//!@tparam Capacity The maximum number of elements static_vector can store, fixed at compile time. +template +class static_vector + : public vector > +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + typedef vector > base_t; + + BOOST_COPYABLE_AND_MOVABLE(static_vector) + + template + friend class static_vector; + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +public: + //! @brief The type of elements stored in the container. + typedef typename base_t::value_type value_type; + //! @brief The unsigned integral type used by the container. + typedef typename base_t::size_type size_type; + //! @brief The pointers difference type. + typedef typename base_t::difference_type difference_type; + //! @brief The pointer type. + typedef typename base_t::pointer pointer; + //! @brief The const pointer type. + typedef typename base_t::const_pointer const_pointer; + //! @brief The value reference type. + typedef typename base_t::reference reference; + //! @brief The value const reference type. + typedef typename base_t::const_reference const_reference; + //! @brief The iterator type. + typedef typename base_t::iterator iterator; + //! @brief The const iterator type. + typedef typename base_t::const_iterator const_iterator; + //! @brief The reverse iterator type. + typedef typename base_t::reverse_iterator reverse_iterator; + //! @brief The const reverse iterator. + typedef typename base_t::const_reverse_iterator const_reverse_iterator; + + //! @brief Constructs an empty static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + static_vector() BOOST_CONTAINER_NOEXCEPT + : base_t() + {} + + //! @pre count <= capacity() + //! + //! @brief Constructs a static_vector containing count value initialized values. + //! + //! @param count The number of values which will be contained in the container. + //! + //! @par Throws + //! If Value's value initialization throws. + //! + //! @par Complexity + //! Linear O(N). + explicit static_vector(size_type count) + : base_t(count) + {} + + //! @pre count <= capacity() + //! + //! @brief Constructs a static_vector containing count default initialized values. + //! + //! @param count The number of values which will be contained in the container. + //! + //! @par Throws + //! If Value's default initialization throws. + //! + //! @par Complexity + //! Linear O(N). + //! + //! @par Note + //! Non-standard extension + static_vector(size_type count, default_init_t) + : base_t(count, default_init_t()) + {} + + //! @pre count <= capacity() + //! + //! @brief Constructs a static_vector containing count copies of value. + //! + //! @param count The number of copies of a values that will be contained in the container. + //! @param value The value which will be used to copy construct values. + //! + //! @par Throws + //! If Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector(size_type count, value_type const& value) + : base_t(count, value) + {} + + //! @pre + //! @li distance(first, last) <= capacity() + //! @li Iterator must meet the \c ForwardTraversalIterator concept. + //! + //! @brief Constructs a static_vector containing copy of a range [first, last). + //! + //! @param first The iterator to the first element in range. + //! @param last The iterator to the one after the last element in range. + //! + //! @par Throws + //! If Value's constructor taking a dereferenced Iterator throws. + //! + //! @par Complexity + //! Linear O(N). + template + static_vector(Iterator first, Iterator last) + : base_t(first, last) + {} + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre + //! @li distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Constructs a static_vector containing copy of a range [il.begin(), il.end()). + //! + //! @param il std::initializer_list with values to initialize vector. + //! + //! @par Throws + //! If Value's constructor taking a dereferenced std::initializer_list throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector(std::initializer_list il) + : base_t(il) + {} +#endif + + //! @brief Constructs a copy of other static_vector. + //! + //! @param other The static_vector which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector(static_vector const& other) + : base_t(other) + {} + + //! @pre other.size() <= capacity(). + //! + //! @brief Constructs a copy of other static_vector. + //! + //! @param other The static_vector which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + template + static_vector(static_vector const& other) + : base_t(other) + {} + + //! @brief Move constructor. Moves Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be moved to this one. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor throws. + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector(BOOST_RV_REF(static_vector) other) + : base_t(boost::move(static_cast(other))) + {} + + //! @pre other.size() <= capacity() + //! + //! @brief Move constructor. Moves Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be moved to this one. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor throws. + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + template + static_vector(BOOST_RV_REF_BEG static_vector BOOST_RV_REF_END other) + : base_t(boost::move(static_cast::base_t&>(other))) + {} + + //! @brief Copy assigns Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector & operator=(BOOST_COPY_ASSIGN_REF(static_vector) other) + { + return static_cast(base_t::operator=(static_cast(other))); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @brief Copy assigns Values stored in std::initializer_list to *this. + //! + //! @param il The std::initializer_list which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector & operator=(std::initializer_list il) + { + return static_cast(base_t::operator=(il)); + } +#endif + + //! @pre other.size() <= capacity() + //! + //! @brief Copy assigns Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be copied to this one. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + template + static_vector & operator=(static_vector const& other) + { + return static_cast(base_t::operator= + (static_cast::base_t const&>(other))); + } + + //! @brief Move assignment. Moves Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be moved to this one. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor or move assignment throws. + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + static_vector & operator=(BOOST_RV_REF(static_vector) other) + { + return static_cast(base_t::operator=(boost::move(static_cast(other)))); + } + + //! @pre other.size() <= capacity() + //! + //! @brief Move assignment. Moves Values stored in the other static_vector to this one. + //! + //! @param other The static_vector which content will be moved to this one. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor or move assignment throws. + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + template + static_vector & operator=(BOOST_RV_REF_BEG static_vector BOOST_RV_REF_END other) + { + return static_cast(base_t::operator= + (boost::move(static_cast::base_t&>(other)))); + } + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + + //! @brief Destructor. Destroys Values stored in this container. + //! + //! @par Throws + //! Nothing + //! + //! @par Complexity + //! Linear O(N). + ~static_vector(); + + //! @brief Swaps contents of the other static_vector and this one. + //! + //! @param other The static_vector which content will be swapped with this one's content. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor or move assignment throws, + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor or copy assignment throws, + //! + //! @par Complexity + //! Linear O(N). + void swap(static_vector & other); + + //! @pre other.size() <= capacity() && size() <= other.capacity() + //! + //! @brief Swaps contents of the other static_vector and this one. + //! + //! @param other The static_vector which content will be swapped with this one's content. + //! + //! @par Throws + //! @li If \c boost::has_nothrow_move::value is \c true and Value's move constructor or move assignment throws, + //! @li If \c boost::has_nothrow_move::value is \c false and Value's copy constructor or copy assignment throws, + //! + //! @par Complexity + //! Linear O(N). + template + void swap(static_vector & other); + + //! @pre count <= capacity() + //! + //! @brief Inserts or erases elements at the end such that + //! the size becomes count. New elements are value initialized. + //! + //! @param count The number of elements which will be stored in the container. + //! + //! @par Throws + //! If Value's value initialization throws. + //! + //! @par Complexity + //! Linear O(N). + void resize(size_type count); + + //! @pre count <= capacity() + //! + //! @brief Inserts or erases elements at the end such that + //! the size becomes count. New elements are default initialized. + //! + //! @param count The number of elements which will be stored in the container. + //! + //! @par Throws + //! If Value's default initialization throws. + //! + //! @par Complexity + //! Linear O(N). + //! + //! @par Note + //! Non-standard extension + void resize(size_type count, default_init_t); + + //! @pre count <= capacity() + //! + //! @brief Inserts or erases elements at the end such that + //! the size becomes count. New elements are copy constructed from value. + //! + //! @param count The number of elements which will be stored in the container. + //! @param value The value used to copy construct the new element. + //! + //! @par Throws + //! If Value's copy constructor throws. + //! + //! @par Complexity + //! Linear O(N). + void resize(size_type count, value_type const& value); + + //! @pre count <= capacity() + //! + //! @brief This call has no effect because the Capacity of this container is constant. + //! + //! @param count The number of elements which the container should be able to contain. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Linear O(N). + void reserve(size_type count) BOOST_CONTAINER_NOEXCEPT; + + //! @pre size() < capacity() + //! + //! @brief Adds a copy of value at the end. + //! + //! @param value The value used to copy construct the new element. + //! + //! @par Throws + //! If Value's copy constructor throws. + //! + //! @par Complexity + //! Constant O(1). + void push_back(value_type const& value); + + //! @pre size() < capacity() + //! + //! @brief Moves value to the end. + //! + //! @param value The value to move construct the new element. + //! + //! @par Throws + //! If Value's move constructor throws. + //! + //! @par Complexity + //! Constant O(1). + void push_back(BOOST_RV_REF(value_type) value); + + //! @pre !empty() + //! + //! @brief Destroys last value and decreases the size. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + void pop_back(); + + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li size() < capacity() + //! + //! @brief Inserts a copy of element at p. + //! + //! @param p The position at which the new value will be inserted. + //! @param value The value used to copy construct the new element. + //! + //! @par Throws + //! @li If Value's copy constructor or copy assignment throws + //! @li If Value's move constructor or move assignment throws. + //! + //! @par Complexity + //! Constant or linear. + iterator insert(const_iterator p, value_type const& value); + + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li size() < capacity() + //! + //! @brief Inserts a move-constructed element at p. + //! + //! @param p The position at which the new value will be inserted. + //! @param value The value used to move construct the new element. + //! + //! @par Throws + //! If Value's move constructor or move assignment throws. + //! + //! @par Complexity + //! Constant or linear. + iterator insert(const_iterator p, BOOST_RV_REF(value_type) value); + + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li size() + count <= capacity() + //! + //! @brief Inserts a count copies of value at p. + //! + //! @param p The position at which new elements will be inserted. + //! @param count The number of new elements which will be inserted. + //! @param value The value used to copy construct new elements. + //! + //! @par Throws + //! @li If Value's copy constructor or copy assignment throws. + //! @li If Value's move constructor or move assignment throws. + //! + //! @par Complexity + //! Linear O(N). + iterator insert(const_iterator p, size_type count, value_type const& value); + + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li distance(first, last) <= capacity() + //! @li \c Iterator must meet the \c ForwardTraversalIterator concept. + //! + //! @brief Inserts a copy of a range [first, last) at p. + //! + //! @param p The position at which new elements will be inserted. + //! @param first The iterator to the first element of a range used to construct new elements. + //! @param last The iterator to the one after the last element of a range used to construct new elements. + //! + //! @par Throws + //! @li If Value's constructor and assignment taking a dereferenced \c Iterator. + //! @li If Value's move constructor or move assignment throws. + //! + //! @par Complexity + //! Linear O(N). + template + iterator insert(const_iterator p, Iterator first, Iterator last); + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()]. + //! @li distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Inserts a copy of a range [il.begin(), il.end()) at p. + //! + //! @param p The position at which new elements will be inserted. + //! @param il The std::initializer_list which contains elements that will be inserted. + //! + //! @par Throws + //! @li If Value's constructor and assignment taking a dereferenced std::initializer_list iterator. + //! + //! @par Complexity + //! Linear O(N). + iterator insert(const_iterator p, std::initializer_list il); +#endif + + //! @pre \c p must be a valid iterator of \c *this in range [begin(), end()) + //! + //! @brief Erases Value from p. + //! + //! @param p The position of the element which will be erased from the container. + //! + //! @par Throws + //! If Value's move assignment throws. + //! + //! @par Complexity + //! Linear O(N). + iterator erase(const_iterator p); + + //! @pre + //! @li \c first and \c last must define a valid range + //! @li iterators must be in range [begin(), end()] + //! + //! @brief Erases Values from a range [first, last). + //! + //! @param first The position of the first element of a range which will be erased from the container. + //! @param last The position of the one after the last element of a range which will be erased from the container. + //! + //! @par Throws + //! If Value's move assignment throws. + //! + //! @par Complexity + //! Linear O(N). + iterator erase(const_iterator first, const_iterator last); + + //! @pre distance(first, last) <= capacity() + //! + //! @brief Assigns a range [first, last) of Values to this container. + //! + //! @param first The iterator to the first element of a range used to construct new content of this container. + //! @param last The iterator to the one after the last element of a range used to construct new content of this container. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws, + //! + //! @par Complexity + //! Linear O(N). + template + void assign(Iterator first, Iterator last); + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! @pre distance(il.begin(), il.end()) <= capacity() + //! + //! @brief Assigns a range [il.begin(), il.end()) of Values to this container. + //! + //! @param first std::initializer_list with values used to construct new content of this container. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws, + //! + //! @par Complexity + //! Linear O(N). + void assign(std::initializer_list il); +#endif + + //! @pre count <= capacity() + //! + //! @brief Assigns a count copies of value to this container. + //! + //! @param count The new number of elements which will be container in the container. + //! @param value The value which will be used to copy construct the new content. + //! + //! @par Throws + //! If Value's copy constructor or copy assignment throws. + //! + //! @par Complexity + //! Linear O(N). + void assign(size_type count, value_type const& value); + + //! @pre size() < capacity() + //! + //! @brief Inserts a Value constructed with + //! \c std::forward(args)... in the end of the container. + //! + //! @param args The arguments of the constructor of the new element which will be created at the end of the container. + //! + //! @par Throws + //! If in-place constructor throws or Value's move constructor throws. + //! + //! @par Complexity + //! Constant O(1). + template + void emplace_back(Args &&...args); + + //! @pre + //! @li \c p must be a valid iterator of \c *this in range [begin(), end()] + //! @li size() < capacity() + //! + //! @brief Inserts a Value constructed with + //! \c std::forward(args)... before p + //! + //! @param p The position at which new elements will be inserted. + //! @param args The arguments of the constructor of the new element. + //! + //! @par Throws + //! If in-place constructor throws or if Value's move constructor or move assignment throws. + //! + //! @par Complexity + //! Constant or linear. + template + iterator emplace(const_iterator p, Args &&...args); + + //! @brief Removes all elements from the container. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + void clear() BOOST_CONTAINER_NOEXCEPT; + + //! @pre i < size() + //! + //! @brief Returns reference to the i-th element. + //! + //! @param i The element's index. + //! + //! @return reference to the i-th element + //! from the beginning of the container. + //! + //! @par Throws + //! \c std::out_of_range exception by default. + //! + //! @par Complexity + //! Constant O(1). + reference at(size_type i); + + //! @pre i < size() + //! + //! @brief Returns const reference to the i-th element. + //! + //! @param i The element's index. + //! + //! @return const reference to the i-th element + //! from the beginning of the container. + //! + //! @par Throws + //! \c std::out_of_range exception by default. + //! + //! @par Complexity + //! Constant O(1). + const_reference at(size_type i) const; + + //! @pre i < size() + //! + //! @brief Returns reference to the i-th element. + //! + //! @param i The element's index. + //! + //! @return reference to the i-th element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + reference operator[](size_type i); + + //! @pre i < size() + //! + //! @brief Returns const reference to the i-th element. + //! + //! @param i The element's index. + //! + //! @return const reference to the i-th element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + const_reference operator[](size_type i) const; + + //! @pre \c !empty() + //! + //! @brief Returns reference to the first element. + //! + //! @return reference to the first element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + reference front(); + + //! @pre \c !empty() + //! + //! @brief Returns const reference to the first element. + //! + //! @return const reference to the first element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + const_reference front() const; + + //! @pre \c !empty() + //! + //! @brief Returns reference to the last element. + //! + //! @return reference to the last element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + reference back(); + + //! @pre \c !empty() + //! + //! @brief Returns const reference to the first element. + //! + //! @return const reference to the last element + //! from the beginning of the container. + //! + //! @par Throws + //! Nothing by default. + //! + //! @par Complexity + //! Constant O(1). + const_reference back() const; + + //! @brief Pointer such that [data(), data() + size()) is a valid range. + //! For a non-empty vector data() == &front(). + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + Value * data() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Const pointer such that [data(), data() + size()) is a valid range. + //! For a non-empty vector data() == &front(). + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const Value * data() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns iterator to the first element. + //! + //! @return iterator to the first element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + iterator begin() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const iterator to the first element. + //! + //! @return const_iterator to the first element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const iterator to the first element. + //! + //! @return const_iterator to the first element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns iterator to the one after the last element. + //! + //! @return iterator pointing to the one after the last element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + iterator end() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const iterator to the one after the last element. + //! + //! @return const_iterator pointing to the one after the last element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_iterator end() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const iterator to the one after the last element. + //! + //! @return const_iterator pointing to the one after the last element contained in the vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns reverse iterator to the first element of the reversed container. + //! + //! @return reverse_iterator pointing to the beginning + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const reverse iterator to the first element of the reversed container. + //! + //! @return const_reverse_iterator pointing to the beginning + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const reverse iterator to the first element of the reversed container. + //! + //! @return const_reverse_iterator pointing to the beginning + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns reverse iterator to the one after the last element of the reversed container. + //! + //! @return reverse_iterator pointing to the one after the last element + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const reverse iterator to the one after the last element of the reversed container. + //! + //! @return const_reverse_iterator pointing to the one after the last element + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns const reverse iterator to the one after the last element of the reversed container. + //! + //! @return const_reverse_iterator pointing to the one after the last element + //! of the reversed static_vector. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns container's capacity. + //! + //! @return container's capacity. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + static size_type capacity() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns container's capacity. + //! + //! @return container's capacity. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + static size_type max_size() BOOST_CONTAINER_NOEXCEPT; + + //! @brief Returns the number of stored elements. + //! + //! @return Number of elements contained in the container. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + size_type size() const BOOST_CONTAINER_NOEXCEPT; + + //! @brief Queries if the container contains elements. + //! + //! @return true if the number of elements contained in the + //! container is equal to 0. + //! + //! @par Throws + //! Nothing. + //! + //! @par Complexity + //! Constant O(1). + bool empty() const BOOST_CONTAINER_NOEXCEPT; +#else + + friend void swap(static_vector &x, static_vector &y) + { + x.swap(y); + } + +#endif // BOOST_CONTAINER_DOXYGEN_INVOKED + +}; + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! @brief Checks if contents of two static_vectors are equal. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if containers have the same size and elements in both containers are equal. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator== (static_vector const& x, static_vector const& y); + +//! @brief Checks if contents of two static_vectors are not equal. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if containers have different size or elements in both containers are not equal. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator!= (static_vector const& x, static_vector const& y); + +//! @brief Lexicographically compares static_vectors. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if x compares lexicographically less than y. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator< (static_vector const& x, static_vector const& y); + +//! @brief Lexicographically compares static_vectors. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if y compares lexicographically less than x. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator> (static_vector const& x, static_vector const& y); + +//! @brief Lexicographically compares static_vectors. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if y don't compare lexicographically less than x. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator<= (static_vector const& x, static_vector const& y); + +//! @brief Lexicographically compares static_vectors. +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @return \c true if x don't compare lexicographically less than y. +//! +//! @par Complexity +//! Linear O(N). +template +bool operator>= (static_vector const& x, static_vector const& y); + +//! @brief Swaps contents of two static_vectors. +//! +//! This function calls static_vector::swap(). +//! +//! @ingroup static_vector_non_member +//! +//! @param x The first static_vector. +//! @param y The second static_vector. +//! +//! @par Complexity +//! Linear O(N). +template +inline void swap(static_vector & x, static_vector & y); + +#else + +template +inline void swap(static_vector & x, static_vector & y + , typename container_detail::enable_if_c< C1 != C2>::type * = 0) +{ + x.swap(y); +} + +#endif // BOOST_CONTAINER_DOXYGEN_INVOKED + +}} // namespace boost::container + +#include + +#endif // BOOST_CONTAINER_STATIC_VECTOR_HPP diff --git a/boost/container/string.hpp b/boost/container/string.hpp new file mode 100644 index 0000000..1c3cf3b --- /dev/null +++ b/boost/container/string.hpp @@ -0,0 +1,2918 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_STRING_HPP +#define BOOST_CONTAINER_STRING_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +namespace container_detail { +// ------------------------------------------------------------ +// Class basic_string_base. + +// basic_string_base is a helper class that makes it it easier to write +// an exception-safe version of basic_string. The constructor allocates, +// but does not initialize, a block of memory. The destructor +// deallocates, but does not destroy elements within, a block of +// memory. The destructor assumes that the memory either is the internal buffer, +// or else points to a block of memory that was allocated using string_base's +// allocator and whose size is this->m_storage. +template +class basic_string_base +{ + basic_string_base & operator=(const basic_string_base &); + basic_string_base(const basic_string_base &); + + typedef allocator_traits allocator_traits_type; + public: + typedef Allocator allocator_type; + typedef allocator_type stored_allocator_type; + typedef typename allocator_traits_type::pointer pointer; + typedef typename allocator_traits_type::value_type value_type; + typedef typename allocator_traits_type::size_type size_type; + typedef ::boost::intrusive::pointer_traits pointer_traits; + + basic_string_base() + : members_() + { init(); } + + basic_string_base(const allocator_type& a) + : members_(a) + { init(); } + + basic_string_base(BOOST_RV_REF(allocator_type) a) + : members_(boost::move(a)) + { this->init(); } + + basic_string_base(const allocator_type& a, size_type n) + : members_(a) + { + this->init(); + this->allocate_initial_block(n); + } + + ~basic_string_base() + { + if(!this->is_short()){ + this->deallocate_block(); + this->is_short(true); + } + } + + private: + + //This is the structure controlling a long string + struct long_t + { + size_type is_short : 1; + size_type length : (sizeof(size_type)*CHAR_BIT - 1); + size_type storage; + pointer start; + + long_t() + {} + + long_t(const long_t &other) + { + this->is_short = other.is_short; + length = other.length; + storage = other.storage; + start = other.start; + } + + long_t &operator =(const long_t &other) + { + this->is_short = other.is_short; + length = other.length; + storage = other.storage; + start = other.start; + return *this; + } + }; + + //This type is the first part of the structure controlling a short string + //The "data" member stores + struct short_header + { + unsigned char is_short : 1; + unsigned char length : (CHAR_BIT - 1); + }; + + //This type has the same alignment and size as long_t but it's POD + //so, unlike long_t, it can be placed in a union + + typedef typename boost::aligned_storage< sizeof(long_t), + container_detail::alignment_of::value>::type long_raw_t; + + protected: + static const size_type MinInternalBufferChars = 8; + static const size_type AlignmentOfValueType = + alignment_of::value; + static const size_type ShortDataOffset = + container_detail::ct_rounded_size::value; + static const size_type ZeroCostInternalBufferChars = + (sizeof(long_t) - ShortDataOffset)/sizeof(value_type); + static const size_type UnalignedFinalInternalBufferChars = + (ZeroCostInternalBufferChars > MinInternalBufferChars) ? + ZeroCostInternalBufferChars : MinInternalBufferChars; + + struct short_t + { + short_header h; + value_type data[UnalignedFinalInternalBufferChars]; + }; + + union repr_t + { + long_raw_t r; + short_t s; + + const short_t &short_repr() const + { return s; } + + const long_t &long_repr() const + { return *static_cast(static_cast(&r)); } + + short_t &short_repr() + { return s; } + + long_t &long_repr() + { return *static_cast(static_cast(&r)); } + }; + + struct members_holder + : public Allocator + { + members_holder() + : Allocator() + {} + + template + explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a) + : Allocator(boost::forward(a)) + {} + + repr_t m_repr; + } members_; + + const Allocator &alloc() const + { return members_; } + + Allocator &alloc() + { return members_; } + + static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type); + + private: + + static const size_type MinAllocation = InternalBufferChars*2; + + protected: + bool is_short() const + { return static_cast(this->members_.m_repr.s.h.is_short != 0); } + + void is_short(bool yes) + { + const bool was_short = this->is_short(); + if(yes && !was_short){ + allocator_traits_type::destroy + ( this->alloc() + , static_cast(static_cast(&this->members_.m_repr.r)) + ); + this->members_.m_repr.s.h.is_short = true; + } + else if(!yes && was_short){ + allocator_traits_type::construct + ( this->alloc() + , static_cast(static_cast(&this->members_.m_repr.r)) + ); + this->members_.m_repr.s.h.is_short = false; + } + } + + private: + void init() + { + this->members_.m_repr.s.h.is_short = 1; + this->members_.m_repr.s.h.length = 0; + } + + protected: + + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + typedef container_detail::integral_constant::value> alloc_version; + + std::pair + allocation_command(allocation_type command, + size_type limit_size, + size_type preferred_size, + size_type &received_size, pointer reuse = 0) + { + if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){ + reuse = pointer(); + command &= ~(expand_fwd | expand_bwd); + } + return container_detail::allocator_version_traits::allocation_command + (this->alloc(), command, limit_size, preferred_size, received_size, reuse); + } + + size_type next_capacity(size_type additional_objects) const + { + return next_capacity_calculator + :: + get( allocator_traits_type::max_size(this->alloc()) + , this->priv_storage(), additional_objects ); + } + + void deallocate(pointer p, size_type n) + { + if (p && (n > InternalBufferChars)) + this->alloc().deallocate(p, n); + } + + void construct(pointer p, const value_type &value = value_type()) + { + allocator_traits_type::construct + ( this->alloc() + , container_detail::to_raw_pointer(p) + , value + ); + } + + void destroy(pointer p, size_type n) + { + value_type *raw_p = container_detail::to_raw_pointer(p); + for(; n--; ++raw_p){ + allocator_traits_type::destroy( this->alloc(), raw_p); + } + } + + void destroy(pointer p) + { + allocator_traits_type::destroy + ( this->alloc() + , container_detail::to_raw_pointer(p) + ); + } + + void allocate_initial_block(size_type n) + { + if (n <= this->max_size()) { + if(n > InternalBufferChars){ + size_type new_cap = this->next_capacity(n); + pointer p = this->allocation_command(allocate_new, n, new_cap, new_cap).first; + this->is_short(false); + this->priv_long_addr(p); + this->priv_long_size(0); + this->priv_storage(new_cap); + } + } + else{ + throw_length_error("basic_string::allocate_initial_block max_size() exceeded"); + } + } + + void deallocate_block() + { this->deallocate(this->priv_addr(), this->priv_storage()); } + + size_type max_size() const + { return allocator_traits_type::max_size(this->alloc()) - 1; } + + protected: + size_type priv_capacity() const + { return this->priv_storage() - 1; } + + pointer priv_short_addr() const + { return pointer_traits::pointer_to(const_cast(this->members_.m_repr.short_repr().data[0])); } + + pointer priv_long_addr() const + { return this->members_.m_repr.long_repr().start; } + + pointer priv_addr() const + { + return this->is_short() + ? priv_short_addr() + : priv_long_addr() + ; + } + + pointer priv_end_addr() const + { + return this->is_short() + ? this->priv_short_addr() + this->priv_short_size() + : this->priv_long_addr() + this->priv_long_size() + ; + } + + void priv_long_addr(pointer addr) + { this->members_.m_repr.long_repr().start = addr; } + + size_type priv_storage() const + { return this->is_short() ? priv_short_storage() : priv_long_storage(); } + + size_type priv_short_storage() const + { return InternalBufferChars; } + + size_type priv_long_storage() const + { return this->members_.m_repr.long_repr().storage; } + + void priv_storage(size_type storage) + { + if(!this->is_short()) + this->priv_long_storage(storage); + } + + void priv_long_storage(size_type storage) + { + this->members_.m_repr.long_repr().storage = storage; + } + + size_type priv_size() const + { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); } + + size_type priv_short_size() const + { return this->members_.m_repr.short_repr().h.length; } + + size_type priv_long_size() const + { return this->members_.m_repr.long_repr().length; } + + void priv_size(size_type sz) + { + if(this->is_short()) + this->priv_short_size(sz); + else + this->priv_long_size(sz); + } + + void priv_short_size(size_type sz) + { + this->members_.m_repr.s.h.length = (unsigned char)sz; + } + + void priv_long_size(size_type sz) + { + this->members_.m_repr.long_repr().length = sz; + } + + void swap_data(basic_string_base& other) + { + if(this->is_short()){ + if(other.is_short()){ + std::swap(this->members_.m_repr, other.members_.m_repr); + } + else{ + short_t short_backup(this->members_.m_repr.short_repr()); + long_t long_backup (other.members_.m_repr.long_repr()); + other.members_.m_repr.long_repr().~long_t(); + ::new(&this->members_.m_repr.long_repr()) long_t; + this->members_.m_repr.long_repr() = long_backup; + other.members_.m_repr.short_repr() = short_backup; + } + } + else{ + if(other.is_short()){ + short_t short_backup(other.members_.m_repr.short_repr()); + long_t long_backup (this->members_.m_repr.long_repr()); + this->members_.m_repr.long_repr().~long_t(); + ::new(&other.members_.m_repr.long_repr()) long_t; + other.members_.m_repr.long_repr() = long_backup; + this->members_.m_repr.short_repr() = short_backup; + } + else{ + boost::container::swap_dispatch(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr()); + } + } + } +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! The basic_string class represents a Sequence of characters. It contains all the +//! usual operations of a Sequence, and, additionally, it contains standard string +//! operations such as search and concatenation. +//! +//! The basic_string class is parameterized by character type, and by that type's +//! Character Traits. +//! +//! This class has performance characteristics very much like vector<>, meaning, +//! for example, that it does not perform reference-count or copy-on-write, and that +//! concatenation of two strings is an O(N) operation. +//! +//! Some of basic_string's member functions use an unusual method of specifying positions +//! and ranges. In addition to the conventional method using iterators, many of +//! basic_string's member functions use a single value pos of type size_type to represent a +//! position (in which case the position is begin() + pos, and many of basic_string's +//! member functions use two values, pos and n, to represent a range. In that case pos is +//! the beginning of the range and n is its size. That is, the range is +//! [begin() + pos, begin() + pos + n). +//! +//! Note that the C++ standard does not specify the complexity of basic_string operations. +//! In this implementation, basic_string has performance characteristics very similar to +//! those of vector: access to a single character is O(1), while copy and concatenation +//! are O(N). +//! +//! In this implementation, begin(), +//! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators. +//! In this implementation, iterators are only invalidated by member functions that +//! explicitly change the string's contents. +//! +//! \tparam CharT The type of character it contains. +//! \tparam Traits The Character Traits type, which encapsulates basic character operations +//! \tparam Allocator The allocator, used for internal memory management. +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED +template , class Allocator = std::allocator > +#else +template +#endif +class basic_string + : private container_detail::basic_string_base +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + typedef allocator_traits allocator_traits_type; + BOOST_COPYABLE_AND_MOVABLE(basic_string) + typedef container_detail::basic_string_base base_t; + static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars; + + protected: + // Allocator helper class to use a char_traits as a function object. + + template + struct Eq_traits + { + //Compatibility with std::binary_function + typedef typename Tr::char_type first_argument_type; + typedef typename Tr::char_type second_argument_type; + typedef bool result_type; + + bool operator()(const first_argument_type& x, const second_argument_type& y) const + { return Tr::eq(x, y); } + }; + + template + struct Not_within_traits + { + typedef typename Tr::char_type argument_type; + typedef bool result_type; + + typedef const typename Tr::char_type* Pointer; + const Pointer m_first; + const Pointer m_last; + + Not_within_traits(Pointer f, Pointer l) + : m_first(f), m_last(l) {} + + bool operator()(const typename Tr::char_type& x) const + { + return std::find_if(m_first, m_last, + std::bind1st(Eq_traits(), x)) == m_last; + } + }; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + typedef Traits traits_type; + typedef CharT value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type; + typedef BOOST_CONTAINER_IMPDEF(pointer) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) const_reverse_iterator; + static const size_type npos = size_type(-1); + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + typedef constant_iterator cvalue_iterator; + typedef typename base_t::allocator_v1 allocator_v1; + typedef typename base_t::allocator_v2 allocator_v2; + typedef typename base_t::alloc_version alloc_version; + typedef ::boost::intrusive::pointer_traits pointer_traits; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: // Constructor, destructor, assignment. + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + struct reserve_t {}; + + basic_string(reserve_t, size_type n, + const allocator_type& a = allocator_type()) + //Select allocator as in copy constructor as reserve_t-based constructors + //are two step copies optimized for capacity + : base_t( allocator_traits_type::select_on_container_copy_construction(a) + , n + 1) + { this->priv_terminate_string(); } + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //! Effects: Default constructs a basic_string. + //! + //! Throws: If allocator_type's default constructor throws. + basic_string() + : base_t() + { this->priv_terminate_string(); } + + + //! Effects: Constructs a basic_string taking the allocator as parameter. + //! + //! Throws: Nothing + explicit basic_string(const allocator_type& a) BOOST_CONTAINER_NOEXCEPT + : base_t(a) + { this->priv_terminate_string(); } + + //! Effects: Copy constructs a basic_string. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor or allocation throws. + basic_string(const basic_string& s) + : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc())) + { + this->priv_terminate_string(); + this->assign(s.begin(), s.end()); + } + + //! Effects: Move constructor. Moves s's resources to *this. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + basic_string(BOOST_RV_REF(basic_string) s) BOOST_CONTAINER_NOEXCEPT + : base_t(boost::move(s.alloc())) + { + if(s.alloc() == this->alloc()){ + this->swap_data(s); + } + else{ + this->assign(s.begin(), s.end()); + } + } + + //! Effects: Copy constructs a basic_string using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocation throws. + basic_string(const basic_string& s, const allocator_type &a) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(s.begin(), s.end()); + } + + //! Effects: Move constructor using the specified allocator. + //! Moves s's resources to *this. + //! + //! Throws: If allocation throws. + //! + //! Complexity: Constant if a == s.get_allocator(), linear otherwise. + basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a) + : base_t(a) + { + this->priv_terminate_string(); + if(a == this->alloc()){ + this->swap_data(s); + } + else{ + this->assign(s.begin(), s.end()); + } + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and is initialized by a specific number of characters of the s string. + basic_string(const basic_string& s, size_type pos, size_type n = npos, + const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + if (pos > s.size()) + throw_out_of_range("basic_string::basic_string out of range position"); + else + this->assign + (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos)); + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and is initialized by a specific number of characters of the s c-string. + basic_string(const CharT* s, size_type n, const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(s, s + n); + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and is initialized by the null-terminated s c-string. + basic_string(const CharT* s, const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(s, s + Traits::length(s)); + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and is initialized by n copies of c. + basic_string(size_type n, CharT c, const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(n, c); + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and is initialized by n default-initialized characters. + basic_string(size_type n, default_init_t, const allocator_type& a = allocator_type()) + : base_t(a, n + 1) + { + this->priv_size(n); + this->priv_terminate_string(); + } + + //! Effects: Constructs a basic_string taking the allocator as parameter, + //! and a range of iterators. + template + basic_string(InputIterator f, InputIterator l, const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(f, l); + } + + //! Effects: Destroys the basic_string. All used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + ~basic_string() BOOST_CONTAINER_NOEXCEPT + {} + + //! Effects: Copy constructs a string. + //! + //! Postcondition: x == *this. + //! + //! Complexity: Linear to the elements x contains. + basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x) + { + if (&x != this){ + allocator_type &this_alloc = this->alloc(); + const allocator_type &x_alloc = x.alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + if(!this->is_short()){ + this->deallocate_block(); + this->is_short(true); + Traits::assign(*this->priv_addr(), CharT(0)); + this->priv_short_size(0); + } + } + container_detail::assign_alloc(this->alloc(), x.alloc(), flag); + this->assign(x.begin(), x.end()); + } + return *this; + } + + //! Effects: Move constructor. Moves x's resources to *this. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and allocation throws + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + basic_string& operator=(BOOST_RV_REF(basic_string) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + //for move constructor, no aliasing (&x != this) is assummed. + BOOST_ASSERT(this != &x); + allocator_type &this_alloc = this->alloc(); + allocator_type &x_alloc = x.alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + container_detail::bool_ flag; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy objects but retain memory in case x reuses it in the future + this->clear(); + //Move allocator if needed + container_detail::move_alloc(this_alloc, x_alloc, flag); + //Nothrow swap + this->swap_data(x); + } + //Else do a one by one move + else{ + this->assign( x.begin(), x.end()); + } + return *this; + } + + //! Effects: Assignment from a null-terminated c-string. + basic_string& operator=(const CharT* s) + { return this->assign(s, s + Traits::length(s)); } + + //! Effects: Assignment from character. + basic_string& operator=(CharT c) + { return this->assign(static_cast(1), c); } + + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return this->alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return this->priv_addr(); } + + //! Effects: Returns a const_iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_addr(); } + + //! Effects: Returns an iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return this->priv_end_addr(); } + + //! Effects: Returns a const_iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_end_addr(); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->priv_end_addr()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->crbegin(); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->priv_addr()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return this->crend(); } + + //! Effects: Returns a const_iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_addr(); } + + //! Effects: Returns a const_iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_end_addr(); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->priv_end_addr()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->priv_addr()); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the vector contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return !this->priv_size(); } + + //! Effects: Returns the number of the elements contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_size(); } + + //! Effects: Returns the number of the elements contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type length() const BOOST_CONTAINER_NOEXCEPT + { return this->size(); } + + //! Effects: Returns the largest possible size of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return base_t::max_size(); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type n, CharT c) + { + if (n <= this->size()) + this->erase(this->begin() + n, this->end()); + else + this->append(n - this->size(), c); + } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type n) + { resize(n, CharT()); } + + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are uninitialized. + //! + //! Throws: If memory allocation throws + //! + //! Complexity: Linear to the difference between size() and new_size. + //! + //! Note: Non-standard extension + void resize(size_type n, default_init_t) + { + if (n <= this->size()) + this->erase(this->begin() + n, this->end()); + else{ + this->priv_reserve(n, false); + this->priv_size(n); + this->priv_terminate_string(); + } + } + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT + { return this->priv_capacity(); } + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws + void reserve(size_type res_arg) + { this->priv_reserve(res_arg); } + + //! Effects: Tries to deallocate the excess of memory created + //! with previous allocations. The size of the string is unchanged + //! + //! Throws: Nothing + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { + //Check if shrinking is possible + if(this->priv_storage() > InternalBufferChars){ + //Check if we should pass from dynamically allocated buffer + //to the internal storage + if(this->priv_size() < InternalBufferChars){ + //Dynamically allocated buffer attributes + pointer long_addr = this->priv_long_addr(); + size_type long_storage = this->priv_long_storage(); + size_type long_size = this->priv_long_size(); + //Shrink from allocated buffer to the internal one, including trailing null + Traits::copy( container_detail::to_raw_pointer(this->priv_short_addr()) + , container_detail::to_raw_pointer(long_addr) + , long_size+1); + this->is_short(true); + this->alloc().deallocate(long_addr, long_storage); + } + else{ + //Shrinking in dynamic buffer + this->priv_shrink_to_fit_dynamic_buffer(alloc_version()); + } + } + } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT + { return *(this->priv_addr() + n); } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT + { return *(this->priv_addr() + n); } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + reference at(size_type n) + { + if (n >= this->size()) + throw_out_of_range("basic_string::at invalid subscript"); + return *(this->priv_addr() + n); + } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + const_reference at(size_type n) const { + if (n >= this->size()) + throw_out_of_range("basic_string::at invalid subscript"); + return *(this->priv_addr() + n); + } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + //! Effects: Calls append(str.data, str.size()). + //! + //! Returns: *this + basic_string& operator+=(const basic_string& s) + { return this->append(s); } + + //! Effects: Calls append(s). + //! + //! Returns: *this + basic_string& operator+=(const CharT* s) + { return this->append(s); } + + //! Effects: Calls append(1, c). + //! + //! Returns: *this + basic_string& operator+=(CharT c) + { this->push_back(c); return *this; } + + //! Effects: Calls append(str.data(), str.size()). + //! + //! Returns: *this + basic_string& append(const basic_string& s) + { return this->append(s.begin(), s.end()); } + + //! Requires: pos <= str.size() + //! + //! Effects: Determines the effective length rlen of the string to append + //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen). + //! + //! Throws: If memory allocation throws and out_of_range if pos > str.size() + //! + //! Returns: *this + basic_string& append(const basic_string& s, size_type pos, size_type n) + { + if (pos > s.size()) + throw_out_of_range("basic_string::append out of range position"); + return this->append(s.begin() + pos, + s.begin() + pos + container_detail::min_value(n, s.size() - pos)); + } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Effects: The function replaces the string controlled by *this with + //! a string of length size() + n whose irst size() elements are a copy of the + //! original string controlled by *this and whose remaining + //! elements are a copy of the initial n elements of s. + //! + //! Throws: If memory allocation throws length_error if size() + n > max_size(). + //! + //! Returns: *this + basic_string& append(const CharT* s, size_type n) + { return this->append(s, s + n); } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Effects: Calls append(s, traits::length(s)). + //! + //! Returns: *this + basic_string& append(const CharT* s) + { return this->append(s, s + Traits::length(s)); } + + //! Effects: Equivalent to append(basic_string(n, c)). + //! + //! Returns: *this + basic_string& append(size_type n, CharT c) + { return this->append(cvalue_iterator(c, n), cvalue_iterator()); } + + //! Requires: [first,last) is a valid range. + //! + //! Effects: Equivalent to append(basic_string(first, last)). + //! + //! Returns: *this + template + basic_string& append(InputIter first, InputIter last) + { this->insert(this->end(), first, last); return *this; } + + //! Effects: Equivalent to append(static_cast(1), c). + void push_back(CharT c) + { + const size_type old_size = this->priv_size(); + if (old_size < this->capacity()){ + const pointer addr = this->priv_addr(); + this->priv_construct_null(addr + old_size + 1); + Traits::assign(addr[old_size], c); + this->priv_size(old_size+1); + } + else{ + //No enough memory, insert a new object at the end + this->append(size_type(1), c); + } + } + + //! Effects: Equivalent to assign(str, 0, npos). + //! + //! Returns: *this + basic_string& assign(const basic_string& s) + { return this->operator=(s); } + + //! Effects: The function replaces the string controlled by *this + //! with a string of length str.size() whose elements are a copy of the string + //! controlled by str. Leaves str in a valid but unspecified state. + //! + //! Throws: Nothing + //! + //! Returns: *this + basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_CONTAINER_NOEXCEPT + { return this->swap_data(ms), *this; } + + //! Requires: pos <= str.size() + //! + //! Effects: Determines the effective length rlen of the string to assign as + //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen). + //! + //! Throws: If memory allocation throws or out_of_range if pos > str.size(). + //! + //! Returns: *this + basic_string& assign(const basic_string& s, size_type pos, size_type n) + { + if (pos > s.size()) + throw_out_of_range("basic_string::assign out of range position"); + return this->assign(s.begin() + pos, + s.begin() + pos + container_detail::min_value(n, s.size() - pos)); + } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Effects: Replaces the string controlled by *this with a string of + //! length n whose elements are a copy of those pointed to by s. + //! + //! Throws: If memory allocation throws or length_error if n > max_size(). + //! + //! Returns: *this + basic_string& assign(const CharT* s, size_type n) + { return this->assign(s, s + n); } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Effects: Calls assign(s, traits::length(s)). + //! + //! Returns: *this + basic_string& assign(const CharT* s) + { return this->assign(s, s + Traits::length(s)); } + + //! Effects: Equivalent to assign(basic_string(n, c)). + //! + //! Returns: *this + basic_string& assign(size_type n, CharT c) + { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); } + + //! Effects: Equivalent to assign(basic_string(first, last)). + //! + //! Returns: *this + basic_string& assign(const CharT* first, const CharT* last) + { + size_type n = static_cast(last - first); + this->reserve(n); + CharT* ptr = container_detail::to_raw_pointer(this->priv_addr()); + Traits::copy(ptr, first, n); + this->priv_construct_null(ptr + n); + this->priv_size(n); + return *this; + } + + //! Effects: Equivalent to assign(basic_string(first, last)). + //! + //! Returns: *this + template + basic_string& assign(InputIter first, InputIter last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + >::type * = 0 + #endif + ) + { + size_type cur = 0; + const pointer addr = this->priv_addr(); + CharT *ptr = container_detail::to_raw_pointer(addr); + const size_type old_size = this->priv_size(); + while (first != last && cur != old_size) { + Traits::assign(*ptr, *first); + ++first; + ++cur; + ++ptr; + } + if (first == last) + this->erase(addr + cur, addr + old_size); + else + this->append(first, last); + return *this; + } + + //! Requires: pos <= size(). + //! + //! Effects: Calls insert(pos, str.data(), str.size()). + //! + //! Throws: If memory allocation throws or out_of_range if pos > size(). + //! + //! Returns: *this + basic_string& insert(size_type pos, const basic_string& s) + { + const size_type sz = this->size(); + if (pos > sz) + throw_out_of_range("basic_string::insert out of range position"); + if (sz > this->max_size() - s.size()) + throw_length_error("basic_string::insert max_size() exceeded"); + this->insert(this->priv_addr() + pos, s.begin(), s.end()); + return *this; + } + + //! Requires: pos1 <= size() and pos2 <= str.size() + //! + //! Effects: Determines the effective length rlen of the string to insert as + //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen). + //! + //! Throws: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size(). + //! + //! Returns: *this + basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n) + { + const size_type sz = this->size(); + const size_type str_size = s.size(); + if (pos1 > sz || pos2 > str_size) + throw_out_of_range("basic_string::insert out of range position"); + size_type len = container_detail::min_value(n, str_size - pos2); + if (sz > this->max_size() - len) + throw_length_error("basic_string::insert max_size() exceeded"); + const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2; + const CharT *end_ptr = beg_ptr + len; + this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr); + return *this; + } + + //! Requires: s points to an array of at least n elements of CharT and pos <= size(). + //! + //! Effects: Replaces the string controlled by *this with a string of length size() + n + //! whose first pos elements are a copy of the initial elements of the original string + //! controlled by *this and whose next n elements are a copy of the elements in s and whose + //! remaining elements are a copy of the remaining elements of the original string controlled by *this. + //! + //! Throws: If memory allocation throws, out_of_range if pos > size() or + //! length_error if size() + n > max_size(). + //! + //! Returns: *this + basic_string& insert(size_type pos, const CharT* s, size_type n) + { + if (pos > this->size()) + throw_out_of_range("basic_string::insert out of range position"); + if (this->size() > this->max_size() - n) + throw_length_error("basic_string::insert max_size() exceeded"); + this->insert(this->priv_addr() + pos, s, s + n); + return *this; + } + + //! Requires: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT + //! + //! Effects: Calls insert(pos, s, traits::length(s)). + //! + //! Throws: If memory allocation throws, out_of_range if pos > size() + //! length_error if size() > max_size() - Traits::length(s) + //! + //! Returns: *this + basic_string& insert(size_type pos, const CharT* s) + { + if (pos > this->size()) + throw_out_of_range("basic_string::insert out of range position"); + size_type len = Traits::length(s); + if (this->size() > this->max_size() - len) + throw_length_error("basic_string::insert max_size() exceeded"); + this->insert(this->priv_addr() + pos, s, s + len); + return *this; + } + + //! Effects: Equivalent to insert(pos, basic_string(n, c)). + //! + //! Throws: If memory allocation throws, out_of_range if pos > size() + //! length_error if size() > max_size() - n + //! + //! Returns: *this + basic_string& insert(size_type pos, size_type n, CharT c) + { + if (pos > this->size()) + throw_out_of_range("basic_string::insert out of range position"); + if (this->size() > this->max_size() - n) + throw_length_error("basic_string::insert max_size() exceeded"); + this->insert(const_iterator(this->priv_addr() + pos), n, c); + return *this; + } + + //! Requires: p is a valid iterator on *this. + //! + //! Effects: inserts a copy of c before the character referred to by p. + //! + //! Returns: An iterator which refers to the copy of the inserted character. + iterator insert(const_iterator p, CharT c) + { + size_type new_offset = p - this->priv_addr(); + this->insert(p, cvalue_iterator(c, 1), cvalue_iterator()); + return this->priv_addr() + new_offset; + } + + + //! Requires: p is a valid iterator on *this. + //! + //! Effects: Inserts n copies of c before the character referred to by p. + //! + //! Returns: an iterator to the first inserted element or p if n is 0. + iterator insert(const_iterator p, size_type n, CharT c) + { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); } + + //! Requires: p is a valid iterator on *this. [first,last) is a valid range. + //! + //! Effects: Equivalent to insert(p - begin(), basic_string(first, last)). + //! + //! Returns: an iterator to the first inserted element or p if first == last. + template + iterator insert(const_iterator p, InputIter first, InputIter last + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + const size_type n_pos = p - this->cbegin(); + for ( ; first != last; ++first, ++p) { + p = this->insert(p, *first); + } + return this->begin() + n_pos; + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator p, ForwardIter first, ForwardIter last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + ) + { + const size_type n_pos = p - this->cbegin(); + if (first != last) { + const size_type n = std::distance(first, last); + const size_type old_size = this->priv_size(); + const size_type remaining = this->capacity() - old_size; + const pointer old_start = this->priv_addr(); + bool enough_capacity = false; + std::pair allocation_ret; + size_type new_cap = 0; + + //Check if we have enough capacity + if (remaining >= n){ + enough_capacity = true; + } + else { + //Otherwise expand current buffer or allocate new storage + new_cap = this->next_capacity(n); + allocation_ret = this->allocation_command + (allocate_new | expand_fwd | expand_bwd, old_size + n + 1, + new_cap, new_cap, old_start); + + //Check forward expansion + if(old_start == allocation_ret.first){ + enough_capacity = true; + this->priv_storage(new_cap); + } + } + + //Reuse same buffer + if(enough_capacity){ + const size_type elems_after = old_size - (p - old_start); + const size_type old_length = old_size; + if (elems_after >= n) { + const pointer pointer_past_last = old_start + old_size + 1; + priv_uninitialized_copy(old_start + (old_size - n + 1), + pointer_past_last, pointer_past_last); + + this->priv_size(old_size+n); + Traits::move(const_cast(container_detail::to_raw_pointer(p + n)), + container_detail::to_raw_pointer(p), + (elems_after - n) + 1); + this->priv_copy(first, last, const_cast(container_detail::to_raw_pointer(p))); + } + else { + ForwardIter mid = first; + std::advance(mid, elems_after + 1); + + priv_uninitialized_copy(mid, last, old_start + old_size + 1); + const size_type newer_size = old_size + (n - elems_after); + this->priv_size(newer_size); + priv_uninitialized_copy + (p, const_iterator(old_start + old_length + 1), + old_start + newer_size); + this->priv_size(newer_size + elems_after); + this->priv_copy(first, mid, const_cast(container_detail::to_raw_pointer(p))); + } + } + else{ + pointer new_start = allocation_ret.first; + if(!allocation_ret.second){ + //Copy data to new buffer + size_type new_length = 0; + //This can't throw, since characters are POD + new_length += priv_uninitialized_copy + (const_iterator(old_start), p, new_start); + new_length += priv_uninitialized_copy + (first, last, new_start + new_length); + new_length += priv_uninitialized_copy + (p, const_iterator(old_start + old_size), + new_start + new_length); + this->priv_construct_null(new_start + new_length); + + this->deallocate_block(); + this->is_short(false); + this->priv_long_addr(new_start); + this->priv_long_size(new_length); + this->priv_long_storage(new_cap); + } + else{ + //value_type is POD, so backwards expansion is much easier + //than with vector + value_type * const oldbuf = container_detail::to_raw_pointer(old_start); + value_type * const newbuf = container_detail::to_raw_pointer(new_start); + const value_type *const pos = container_detail::to_raw_pointer(p); + const size_type before = pos - oldbuf; + + //First move old data + Traits::move(newbuf, oldbuf, before); + Traits::move(newbuf + before + n, pos, old_size - before); + //Now initialize the new data + priv_uninitialized_copy(first, last, new_start + before); + this->priv_construct_null(new_start + (old_size + n)); + this->is_short(false); + this->priv_long_addr(new_start); + this->priv_long_size(old_size + n); + this->priv_long_storage(new_cap); + } + } + } + return this->begin() + n_pos; + } + #endif + + //! Requires: pos <= size() + //! + //! Effects: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos. + //! The function then replaces the string controlled by *this with a string of length size() - xlen + //! whose first pos elements are a copy of the initial elements of the original string controlled by *this, + //! and whose remaining elements are a copy of the elements of the original string controlled by *this + //! beginning at position pos + xlen. + //! + //! Throws: out_of_range if pos > size(). + //! + //! Returns: *this + basic_string& erase(size_type pos = 0, size_type n = npos) + { + if (pos > this->size()) + throw_out_of_range("basic_string::erase out of range position"); + const pointer addr = this->priv_addr(); + erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos)); + return *this; + } + + //! Effects: Removes the character referred to by p. + //! + //! Throws: Nothing + //! + //! Returns: An iterator which points to the element immediately following p prior to the element being + //! erased. If no such element exists, end() is returned. + iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT + { + // The move includes the terminating null. + CharT * const ptr = const_cast(container_detail::to_raw_pointer(p)); + const size_type old_size = this->priv_size(); + Traits::move(ptr, + container_detail::to_raw_pointer(p + 1), + old_size - (p - this->priv_addr())); + this->priv_size(old_size-1); + return iterator(ptr); + } + + //! Requires: first and last are valid iterators on *this, defining a range [first,last). + //! + //! Effects: Removes the characters in the range [first,last). + //! + //! Throws: Nothing + //! + //! Returns: An iterator which points to the element pointed to by last prior to + //! the other elements being erased. If no such element exists, end() is returned. + iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT + { + CharT * f = const_cast(container_detail::to_raw_pointer(first)); + if (first != last) { // The move includes the terminating null. + const size_type num_erased = last - first; + const size_type old_size = this->priv_size(); + Traits::move(f, + container_detail::to_raw_pointer(last), + (old_size + 1)-(last - this->priv_addr())); + const size_type new_length = old_size - num_erased; + this->priv_size(new_length); + } + return iterator(f); + } + + //! Requires: !empty() + //! + //! Throws: Nothing + //! + //! Effects: Equivalent to erase(size() - 1, 1). + void pop_back() BOOST_CONTAINER_NOEXCEPT + { + const size_type old_size = this->priv_size(); + Traits::assign(this->priv_addr()[old_size-1], CharT(0)); + this->priv_size(old_size-1);; + } + + //! Effects: Erases all the elements of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the vector. + void clear() BOOST_CONTAINER_NOEXCEPT + { + if (!this->empty()) { + Traits::assign(*this->priv_addr(), CharT(0)); + this->priv_size(0); + } + } + + //! Requires: pos1 <= size(). + //! + //! Effects: Calls replace(pos1, n1, str.data(), str.size()). + //! + //! Throws: if memory allocation throws or out_of_range if pos1 > size(). + //! + //! Returns: *this + basic_string& replace(size_type pos1, size_type n1, const basic_string& str) + { + if (pos1 > this->size()) + throw_out_of_range("basic_string::replace out of range position"); + const size_type len = container_detail::min_value(n1, this->size() - pos1); + if (this->size() - len >= this->max_size() - str.size()) + throw_length_error("basic_string::replace max_size() exceeded"); + const pointer addr = this->priv_addr(); + return this->replace( const_iterator(addr + pos1) + , const_iterator(addr + pos1 + len) + , str.begin(), str.end()); + } + + //! Requires: pos1 <= size() and pos2 <= str.size(). + //! + //! Effects: Determines the effective length rlen of the string to be + //! inserted as the smaller of n2 and str.size() - pos2 and calls + //! replace(pos1, n1, str.data() + pos2, rlen). + //! + //! Throws: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size(). + //! + //! Returns: *this + basic_string& replace(size_type pos1, size_type n1, + const basic_string& str, size_type pos2, size_type n2) + { + if (pos1 > this->size() || pos2 > str.size()) + throw_out_of_range("basic_string::replace out of range position"); + const size_type len1 = container_detail::min_value(n1, this->size() - pos1); + const size_type len2 = container_detail::min_value(n2, str.size() - pos2); + if (this->size() - len1 >= this->max_size() - len2) + throw_length_error("basic_string::replace max_size() exceeded"); + const pointer addr = this->priv_addr(); + const pointer straddr = str.priv_addr(); + return this->replace(addr + pos1, addr + pos1 + len1, + straddr + pos2, straddr + pos2 + len2); + } + + //! Requires: pos1 <= size() and s points to an array of at least n2 elements of CharT. + //! + //! Effects: Determines the effective length xlen of the string to be removed as the + //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. + //! Otherwise, the function replaces the string controlled by *this with a string of + //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements + //! of the original string controlled by *this, whose next n2 elements are a copy of the + //! initial n2 elements of s, and whose remaining elements are a copy of the elements of + //! the original string controlled by *this beginning at position pos + xlen. + //! + //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error + //! if the length of the resulting string would exceed max_size() + //! + //! Returns: *this + basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2) + { + if (pos1 > this->size()) + throw_out_of_range("basic_string::replace out of range position"); + const size_type len = container_detail::min_value(n1, this->size() - pos1); + if (n2 > this->max_size() || size() - len >= this->max_size() - n2) + throw_length_error("basic_string::replace max_size() exceeded"); + const pointer addr = this->priv_addr(); + return this->replace(addr + pos1, addr + pos1 + len, s, s + n2); + } + + //! Requires: pos1 <= size() and s points to an array of at least n2 elements of CharT. + //! + //! Effects: Determines the effective length xlen of the string to be removed as the smaller + //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise, + //! the function replaces the string controlled by *this with a string of length size() - xlen + n2 + //! whose first pos1 elements are a copy of the initial elements of the original string controlled + //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose + //! remaining elements are a copy of the elements of the original string controlled by *this + //! beginning at position pos + xlen. + //! + //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error + //! if the length of the resulting string would exceed max_size() + //! + //! Returns: *this + basic_string& replace(size_type pos, size_type n1, const CharT* s) + { + if (pos > this->size()) + throw_out_of_range("basic_string::replace out of range position"); + const size_type len = container_detail::min_value(n1, this->size() - pos); + const size_type n2 = Traits::length(s); + if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2) + throw_length_error("basic_string::replace max_size() exceeded"); + const pointer addr = this->priv_addr(); + return this->replace(addr + pos, addr + pos + len, + s, s + Traits::length(s)); + } + + //! Requires: pos1 <= size(). + //! + //! Effects: Equivalent to replace(pos1, n1, basic_string(n2, c)). + //! + //! Throws: if memory allocation throws, out_of_range if pos1 > size() or length_error + //! if the length of the resulting string would exceed max_size() + //! + //! Returns: *this + basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c) + { + if (pos1 > this->size()) + throw_out_of_range("basic_string::replace out of range position"); + const size_type len = container_detail::min_value(n1, this->size() - pos1); + if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2) + throw_length_error("basic_string::replace max_size() exceeded"); + const pointer addr = this->priv_addr(); + return this->replace(addr + pos1, addr + pos1 + len, n2, c); + } + + //! Requires: [begin(),i1) and [i1,i2) are valid ranges. + //! + //! Effects: Calls replace(i1 - begin(), i2 - i1, str). + //! + //! Throws: if memory allocation throws + //! + //! Returns: *this + basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str) + { return this->replace(i1, i2, str.begin(), str.end()); } + + //! Requires: [begin(),i1) and [i1,i2) are valid ranges and + //! s points to an array of at least n elements + //! + //! Effects: Calls replace(i1 - begin(), i2 - i1, s, n). + //! + //! Throws: if memory allocation throws + //! + //! Returns: *this + basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n) + { return this->replace(i1, i2, s, s + n); } + + //! Requires: [begin(),i1) and [i1,i2) are valid ranges and s points to an + //! array of at least traits::length(s) + 1 elements of CharT. + //! + //! Effects: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)). + //! + //! Throws: if memory allocation throws + //! + //! Returns: *this + basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s) + { return this->replace(i1, i2, s, s + Traits::length(s)); } + + //! Requires: [begin(),i1) and [i1,i2) are valid ranges. + //! + //! Effects: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)). + //! + //! Throws: if memory allocation throws + //! + //! Returns: *this + basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c) + { + const size_type len = static_cast(i2 - i1); + if (len >= n) { + Traits::assign(const_cast(container_detail::to_raw_pointer(i1)), n, c); + erase(i1 + n, i2); + } + else { + Traits::assign(const_cast(container_detail::to_raw_pointer(i1)), len, c); + insert(i2, n - len, c); + } + return *this; + } + + //! Requires: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges. + //! + //! Effects: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)). + //! + //! Throws: if memory allocation throws + //! + //! Returns: *this + template + basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2 + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0 + #endif + ) + { + for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){ + Traits::assign(*const_cast(container_detail::to_raw_pointer(i1)), *j1); + } + + if (j1 == j2) + this->erase(i1, i2); + else + this->insert(i2, j1, j2); + return *this; + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2 + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + ) + { + difference_type n = std::distance(j1, j2); + const difference_type len = i2 - i1; + if (len >= n) { + this->priv_copy(j1, j2, const_cast(container_detail::to_raw_pointer(i1))); + this->erase(i1 + n, i2); + } + else { + ForwardIter m = j1; + std::advance(m, len); + this->priv_copy(j1, m, const_cast(container_detail::to_raw_pointer(i1))); + this->insert(i2, m, j2); + } + return *this; + } + #endif + + //! Requires: pos <= size() + //! + //! Effects: Determines the effective length rlen of the string to copy as the + //! smaller of n and size() - pos. s shall designate an array of at least rlen elements. + //! The function then replaces the string designated by s with a string of length rlen + //! whose elements are a copy of the string controlled by *this beginning at position pos. + //! The function does not append a null object to the string designated by s. + //! + //! Throws: if memory allocation throws, out_of_range if pos > size(). + //! + //! Returns: rlen + size_type copy(CharT* s, size_type n, size_type pos = 0) const + { + if (pos > this->size()) + throw_out_of_range("basic_string::copy out of range position"); + const size_type len = container_detail::min_value(n, this->size() - pos); + Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len); + return len; + } + + //! Effects: *this contains the same sequence of characters that was in s, + //! s contains the same sequence of characters that was in *this. + //! + //! Throws: Nothing + void swap(basic_string& x) + { + this->base_t::swap_data(x); + container_detail::bool_ flag; + container_detail::swap_alloc(this->alloc(), x.alloc(), flag); + } + + ////////////////////////////////////////////// + // + // data access + // + ////////////////////////////////////////////// + + //! Requires: The program shall not alter any of the values stored in the character array. + //! + //! Returns: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()]. + //! + //! Complexity: constant time. + const CharT* c_str() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::to_raw_pointer(this->priv_addr()); } + + //! Requires: The program shall not alter any of the values stored in the character array. + //! + //! Returns: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()]. + //! + //! Complexity: constant time. + const CharT* data() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::to_raw_pointer(this->priv_addr()); } + + ////////////////////////////////////////////// + // + // string operations + // + ////////////////////////////////////////////// + + //! Effects: Determines the lowest position xpos, if possible, such that both + //! of the following conditions obtain: 19 pos <= xpos and xpos + str.size() <= size(); + //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type find(const basic_string& s, size_type pos = 0) const + { return find(s.c_str(), pos, s.size()); } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find(basic_string(s,n),pos). + size_type find(const CharT* s, size_type pos, size_type n) const + { + if (pos + n > this->size()) + return npos; + else { + const pointer addr = this->priv_addr(); + pointer finish = addr + this->priv_size(); + const const_iterator result = + std::search(container_detail::to_raw_pointer(addr + pos), + container_detail::to_raw_pointer(finish), + s, s + n, Eq_traits()); + return result != finish ? result - begin() : npos; + } + } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find(basic_string(s), pos). + size_type find(const CharT* s, size_type pos = 0) const + { return this->find(s, pos, Traits::length(s)); } + + //! Throws: Nothing + //! + //! Returns: find(basic_string(1,c), pos). + size_type find(CharT c, size_type pos = 0) const + { + const size_type sz = this->size(); + if (pos >= sz) + return npos; + else { + const pointer addr = this->priv_addr(); + pointer finish = addr + sz; + const const_iterator result = + std::find_if(addr + pos, finish, + std::bind2nd(Eq_traits(), c)); + return result != finish ? result - begin() : npos; + } + } + + //! Effects: Determines the highest position xpos, if possible, such + //! that both of the following conditions obtain: + //! a) xpos <= pos and xpos + str.size() <= size(); + //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type rfind(const basic_string& str, size_type pos = npos) const + { return rfind(str.c_str(), pos, str.size()); } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: rfind(basic_string(s, n), pos). + size_type rfind(const CharT* s, size_type pos, size_type n) const + { + const size_type len = this->size(); + + if (n > len) + return npos; + else if (n == 0) + return container_detail::min_value(len, pos); + else { + const const_iterator last = begin() + container_detail::min_value(len - n, pos) + n; + const const_iterator result = find_end(begin(), last, + s, s + n, + Eq_traits()); + return result != last ? result - begin() : npos; + } + } + + //! Requires: pos <= size() and s points to an array of at least + //! traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: rfind(basic_string(s), pos). + size_type rfind(const CharT* s, size_type pos = npos) const + { return rfind(s, pos, Traits::length(s)); } + + //! Throws: Nothing + //! + //! Returns: rfind(basic_string(1,c),pos). + size_type rfind(CharT c, size_type pos = npos) const + { + const size_type len = this->size(); + + if (len < 1) + return npos; + else { + const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; + const_reverse_iterator rresult = + std::find_if(const_reverse_iterator(last), rend(), + std::bind2nd(Eq_traits(), c)); + return rresult != rend() ? (rresult.base() - 1) - begin() : npos; + } + } + + //! Effects: Determines the lowest position xpos, if possible, such that both of the + //! following conditions obtain: a) pos <= xpos and xpos < size(); + //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type find_first_of(const basic_string& s, size_type pos = 0) const + { return find_first_of(s.c_str(), pos, s.size()); } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_first_of(basic_string(s, n), pos). + size_type find_first_of(const CharT* s, size_type pos, size_type n) const + { + const size_type sz = this->size(); + if (pos >= sz) + return npos; + else { + const pointer addr = this->priv_addr(); + pointer finish = addr + sz; + const_iterator result = std::find_first_of + (addr + pos, finish, s, s + n, Eq_traits()); + return result != finish ? result - this->begin() : npos; + } + } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_first_of(basic_string(s), pos). + size_type find_first_of(const CharT* s, size_type pos = 0) const + { return find_first_of(s, pos, Traits::length(s)); } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_first_of(basic_string(1,c), pos). + size_type find_first_of(CharT c, size_type pos = 0) const + { return find(c, pos); } + + //! Effects: Determines the highest position xpos, if possible, such that both of + //! the following conditions obtain: a) xpos <= pos and xpos < size(); b) + //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type find_last_of(const basic_string& str, size_type pos = npos) const + { return find_last_of(str.c_str(), pos, str.size()); } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_last_of(basic_string(s, n), pos). + size_type find_last_of(const CharT* s, size_type pos, size_type n) const + { + const size_type len = this->size(); + + if (len < 1) + return npos; + else { + const pointer addr = this->priv_addr(); + const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1; + const const_reverse_iterator rresult = + std::find_first_of(const_reverse_iterator(last), rend(), + s, s + n, Eq_traits()); + return rresult != rend() ? (rresult.base() - 1) - addr : npos; + } + } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_last_of(basic_string(1,c),pos). + size_type find_last_of(const CharT* s, size_type pos = npos) const + { return find_last_of(s, pos, Traits::length(s)); } + + //! Throws: Nothing + //! + //! Returns: find_last_of(basic_string(s), pos). + size_type find_last_of(CharT c, size_type pos = npos) const + { return rfind(c, pos); } + + //! Effects: Determines the lowest position xpos, if possible, such that + //! both of the following conditions obtain: + //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no + //! element I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type find_first_not_of(const basic_string& str, size_type pos = 0) const + { return find_first_not_of(str.c_str(), pos, str.size()); } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_first_not_of(basic_string(s, n), pos). + size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const + { + if (pos > this->size()) + return npos; + else { + const pointer addr = this->priv_addr(); + const pointer finish = addr + this->priv_size(); + const const_iterator result = std::find_if + (addr + pos, finish, Not_within_traits(s, s + n)); + return result != finish ? result - addr : npos; + } + } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_first_not_of(basic_string(s), pos). + size_type find_first_not_of(const CharT* s, size_type pos = 0) const + { return find_first_not_of(s, pos, Traits::length(s)); } + + //! Throws: Nothing + //! + //! Returns: find_first_not_of(basic_string(1, c), pos). + size_type find_first_not_of(CharT c, size_type pos = 0) const + { + if (pos > this->size()) + return npos; + else { + const pointer addr = this->priv_addr(); + const pointer finish = addr + this->priv_size(); + const const_iterator result + = std::find_if(addr + pos, finish, + std::not1(std::bind2nd(Eq_traits(), c))); + return result != finish ? result - begin() : npos; + } + } + + //! Effects: Determines the highest position xpos, if possible, such that + //! both of the following conditions obtain: a) xpos <= pos and xpos < size(); + //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str. + //! + //! Throws: Nothing + //! + //! Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos. + size_type find_last_not_of(const basic_string& str, size_type pos = npos) const + { return find_last_not_of(str.c_str(), pos, str.size()); } + + //! Requires: s points to an array of at least n elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_last_not_of(basic_string(s, n), pos). + size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const + { + const size_type len = this->size(); + + if (len < 1) + return npos; + else { + const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; + const const_reverse_iterator rresult = + std::find_if(const_reverse_iterator(last), rend(), + Not_within_traits(s, s + n)); + return rresult != rend() ? (rresult.base() - 1) - begin() : npos; + } + } + + //! Requires: s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: Nothing + //! + //! Returns: find_last_not_of(basic_string(s), pos). + size_type find_last_not_of(const CharT* s, size_type pos = npos) const + { return find_last_not_of(s, pos, Traits::length(s)); } + + //! Throws: Nothing + //! + //! Returns: find_last_not_of(basic_string(1, c), pos). + size_type find_last_not_of(CharT c, size_type pos = npos) const + { + const size_type len = this->size(); + + if (len < 1) + return npos; + else { + const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1; + const const_reverse_iterator rresult = + std::find_if(const_reverse_iterator(last), rend(), + std::not1(std::bind2nd(Eq_traits(), c))); + return rresult != rend() ? (rresult.base() - 1) - begin() : npos; + } + } + + //! Requires: Requires: pos <= size() + //! + //! Effects: Determines the effective length rlen of the string to copy as + //! the smaller of n and size() - pos. + //! + //! Throws: If memory allocation throws or out_of_range if pos > size(). + //! + //! Returns: basic_string(data()+pos,rlen). + basic_string substr(size_type pos = 0, size_type n = npos) const + { + if (pos > this->size()) + throw_out_of_range("basic_string::substr out of range position"); + const pointer addr = this->priv_addr(); + return basic_string(addr + pos, + addr + pos + container_detail::min_value(n, size() - pos), this->alloc()); + } + + //! Effects: Determines the effective length rlen of the string to copy as + //! the smaller of size() and str.size(). The function then compares the two strings by + //! calling traits::compare(data(), str.data(), rlen). + //! + //! Throws: Nothing + //! + //! Returns: The nonzero result if the result of the comparison is nonzero. + //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(), + //! and value > 0 if size() > str.size() + int compare(const basic_string& str) const + { + const pointer addr = this->priv_addr(); + const pointer str_addr = str.priv_addr(); + return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size()); + } + + //! Requires: pos1 <= size() + //! + //! Effects: Determines the effective length rlen of the string to copy as + //! the smaller of + //! + //! Throws: out_of_range if pos1 > size() + //! + //! Returns:basic_string(*this,pos1,n1).compare(str). + int compare(size_type pos1, size_type n1, const basic_string& str) const + { + if (pos1 > this->size()) + throw_out_of_range("basic_string::compare out of range position"); + const pointer addr = this->priv_addr(); + const pointer str_addr = str.priv_addr(); + return s_compare(addr + pos1, + addr + pos1 + container_detail::min_value(n1, this->size() - pos1), + str_addr, str_addr + str.priv_size()); + } + + //! Requires: pos1 <= size() and pos2 <= str.size() + //! + //! Effects: Determines the effective length rlen of the string to copy as + //! the smaller of + //! + //! Throws: out_of_range if pos1 > size() or pos2 > str.size() + //! + //! Returns: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)). + int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2) const + { + if (pos1 > this->size() || pos2 > str.size()) + throw_out_of_range("basic_string::compare out of range position"); + const pointer addr = this->priv_addr(); + const pointer str_addr = str.priv_addr(); + return s_compare(addr + pos1, + addr + pos1 + container_detail::min_value(n1, this->size() - pos1), + str_addr + pos2, + str_addr + pos2 + container_detail::min_value(n2, str.size() - pos2)); + } + + //! Throws: Nothing + //! + //! Returns: compare(basic_string(s)). + int compare(const CharT* s) const + { + const pointer addr = this->priv_addr(); + return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s)); + } + + + //! Requires: pos1 > size() and s points to an array of at least n2 elements of CharT. + //! + //! Throws: out_of_range if pos1 > size() + //! + //! Returns: basic_string(*this, pos, n1).compare(basic_string(s, n2)). + int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const + { + if (pos1 > this->size()) + throw_out_of_range("basic_string::compare out of range position"); + const pointer addr = this->priv_addr(); + return s_compare( addr + pos1, + addr + pos1 + container_detail::min_value(n1, this->size() - pos1), + s, s + n2); + } + + //! Requires: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT. + //! + //! Throws: out_of_range if pos1 > size() + //! + //! Returns: basic_string(*this, pos, n1).compare(basic_string(s, n2)). + int compare(size_type pos1, size_type n1, const CharT* s) const + { return this->compare(pos1, n1, s, Traits::length(s)); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + void priv_reserve(size_type res_arg, const bool null_terminate = true) + { + if (res_arg > this->max_size()){ + throw_length_error("basic_string::reserve max_size() exceeded"); + } + + if (this->capacity() < res_arg){ + size_type n = container_detail::max_value(res_arg, this->size()) + 1; + size_type new_cap = this->next_capacity(n); + pointer new_start = this->allocation_command + (allocate_new, n, new_cap, new_cap).first; + size_type new_length = 0; + + const pointer addr = this->priv_addr(); + new_length += priv_uninitialized_copy + (addr, addr + this->priv_size(), new_start); + if(null_terminate){ + this->priv_construct_null(new_start + new_length); + } + this->deallocate_block(); + this->is_short(false); + this->priv_long_addr(new_start); + this->priv_long_size(new_length); + this->priv_storage(new_cap); + } + } + + static int s_compare(const_pointer f1, const_pointer l1, + const_pointer f2, const_pointer l2) + { + const difference_type n1 = l1 - f1; + const difference_type n2 = l2 - f2; + const int cmp = Traits::compare(container_detail::to_raw_pointer(f1), + container_detail::to_raw_pointer(f2), + container_detail::min_value(n1, n2)); + return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0)); + } + + template + void priv_shrink_to_fit_dynamic_buffer + ( AllocVersion + , typename container_detail::enable_if >::type* = 0) + { + //Allocate a new buffer. + size_type real_cap = 0; + const pointer long_addr = this->priv_long_addr(); + const size_type long_size = this->priv_long_size(); + const size_type long_storage = this->priv_long_storage(); + //We can make this nothrow as chars are always NoThrowCopyables + BOOST_TRY{ + const std::pair ret = this->allocation_command + (allocate_new, long_size+1, long_size+1, real_cap, long_addr); + //Copy and update + Traits::copy( container_detail::to_raw_pointer(ret.first) + , container_detail::to_raw_pointer(this->priv_long_addr()) + , long_size+1); + this->priv_long_addr(ret.first); + this->priv_storage(real_cap); + //And release old buffer + this->alloc().deallocate(long_addr, long_storage); + } + BOOST_CATCH(...){ + return; + } + BOOST_CATCH_END + } + + template + void priv_shrink_to_fit_dynamic_buffer + ( AllocVersion + , typename container_detail::enable_if >::type* = 0) + { + size_type received_size; + if(this->alloc().allocation_command + ( shrink_in_place | nothrow_allocation + , this->priv_long_storage(), this->priv_long_size()+1 + , received_size, this->priv_long_addr()).first){ + this->priv_storage(received_size); + } + } + + void priv_construct_null(pointer p) + { this->construct(p, CharT(0)); } + + // Helper functions used by constructors. It is a severe error for + // any of them to be called anywhere except from within constructors. + void priv_terminate_string() + { this->priv_construct_null(this->priv_end_addr()); } + + template inline + void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val) + { + //Save initial position + FwdIt init = first; + + BOOST_TRY{ + //Construct objects + for (; count--; ++first){ + this->construct(first, val); + } + } + BOOST_CATCH(...){ + //Call destructors + for (; init != first; ++init){ + this->destroy(init); + } + BOOST_RETHROW + } + BOOST_CATCH_END + } + + template inline + size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest) + { + //Save initial destination position + FwdIt dest_init = dest; + size_type constructed = 0; + + BOOST_TRY{ + //Try to build objects + for (; first != last; ++dest, ++first, ++constructed){ + this->construct(dest, *first); + } + } + BOOST_CATCH(...){ + //Call destructors + for (; constructed--; ++dest_init){ + this->destroy(dest_init); + } + BOOST_RETHROW + } + BOOST_CATCH_END + return (constructed); + } + + template + void priv_copy(InputIterator first, InputIterator last, OutIterator result) + { + for ( ; first != last; ++first, ++result) + Traits::assign(*result, *first); + } + + void priv_copy(const CharT* first, const CharT* last, CharT* result) + { Traits::copy(result, first, last - first); } + + template + basic_string& priv_replace_dispatch(const_iterator first, const_iterator last, + Integer n, Integer x, + container_detail::true_) + { return this->replace(first, last, (size_type) n, (CharT) x); } + + template + basic_string& priv_replace_dispatch(const_iterator first, const_iterator last, + InputIter f, InputIter l, + container_detail::false_) + { + typedef typename std::iterator_traits::iterator_category Category; + return this->priv_replace(first, last, f, l, Category()); + } + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED + +//!Typedef for a basic_string of +//!narrow characters +typedef basic_string + + ,std::allocator > +string; + +//!Typedef for a basic_string of +//!narrow characters +typedef basic_string + + ,std::allocator > +wstring; + +#endif + +// ------------------------------------------------------------ +// Non-member functions. + +// Operator+ + +template inline + basic_string + operator+(const basic_string& x + ,const basic_string& y) +{ + typedef basic_string str_t; + typedef typename str_t::reserve_t reserve_t; + reserve_t reserve; + str_t result(reserve, x.size() + y.size(), x.get_stored_allocator()); + result.append(x); + result.append(y); + return result; +} + +template inline + basic_string operator+ + ( BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END mx + , BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END my) +{ + mx += my; + return boost::move(mx); +} + +template inline + basic_string operator+ + ( BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END mx + , const basic_string& y) +{ + mx += y; + return boost::move(mx); +} + +template inline + basic_string operator+ + (const basic_string& x + ,BOOST_RV_REF_BEG basic_string BOOST_RV_REF_END my) +{ + my.insert(my.begin(), x.begin(), x.end()); + return boost::move(my); +} + +template inline + basic_string operator+ + (const CharT* s, basic_string y) +{ + y.insert(y.begin(), s, s + Traits::length(s)); + return y; +} + +template inline + basic_string operator+ + (basic_string x, const CharT* s) +{ + x += s; + return x; +} + +template inline + basic_string operator+ + (CharT c, basic_string y) +{ + y.insert(y.begin(), c); + return y; +} + +template inline + basic_string operator+ + (basic_string x, const CharT c) +{ + x += c; + return x; +} + +// Operator== and operator!= + +template +inline bool +operator==(const basic_string& x, + const basic_string& y) +{ + return x.size() == y.size() && + Traits::compare(x.data(), y.data(), x.size()) == 0; +} + +template +inline bool +operator==(const CharT* s, const basic_string& y) +{ + typename basic_string::size_type n = Traits::length(s); + return n == y.size() && Traits::compare(s, y.data(), n) == 0; +} + +template +inline bool +operator==(const basic_string& x, const CharT* s) +{ + typename basic_string::size_type n = Traits::length(s); + return x.size() == n && Traits::compare(x.data(), s, n) == 0; +} + +template +inline bool +operator!=(const basic_string& x, + const basic_string& y) + { return !(x == y); } + +template +inline bool +operator!=(const CharT* s, const basic_string& y) + { return !(s == y); } + +template +inline bool +operator!=(const basic_string& x, const CharT* s) + { return !(x == s); } + + +// Operator< (and also >, <=, and >=). + +template +inline bool +operator<(const basic_string& x, const basic_string& y) +{ + return x.compare(y) < 0; +// return basic_string +// ::s_compare(x.begin(), x.end(), y.begin(), y.end()) < 0; +} + +template +inline bool +operator<(const CharT* s, const basic_string& y) +{ + return y.compare(s) > 0; +// basic_string::size_type n = Traits::length(s); +// return basic_string +// ::s_compare(s, s + n, y.begin(), y.end()) < 0; +} + +template +inline bool +operator<(const basic_string& x, + const CharT* s) +{ + return x.compare(s) < 0; +// basic_string::size_type n = Traits::length(s); +// return basic_string +// ::s_compare(x.begin(), x.end(), s, s + n) < 0; +} + +template +inline bool +operator>(const basic_string& x, + const basic_string& y) { + return y < x; +} + +template +inline bool +operator>(const CharT* s, const basic_string& y) { + return y < s; +} + +template +inline bool +operator>(const basic_string& x, const CharT* s) +{ + return s < x; +} + +template +inline bool +operator<=(const basic_string& x, + const basic_string& y) +{ + return !(y < x); +} + +template +inline bool +operator<=(const CharT* s, const basic_string& y) + { return !(y < s); } + +template +inline bool +operator<=(const basic_string& x, const CharT* s) + { return !(s < x); } + +template +inline bool +operator>=(const basic_string& x, + const basic_string& y) + { return !(x < y); } + +template +inline bool +operator>=(const CharT* s, const basic_string& y) + { return !(s < y); } + +template +inline bool +operator>=(const basic_string& x, const CharT* s) + { return !(x < s); } + +// Swap. +template +inline void swap(basic_string& x, basic_string& y) +{ x.swap(y); } + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +// I/O. +namespace container_detail { + +template +inline bool +string_fill(std::basic_ostream& os, + std::basic_streambuf* buf, + std::size_t n) +{ + CharT f = os.fill(); + std::size_t i; + bool ok = true; + + for (i = 0; i < n; i++) + ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof()); + return ok; +} + +} //namespace container_detail { +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +template +std::basic_ostream& +operator<<(std::basic_ostream& os, const basic_string& s) +{ + typename std::basic_ostream::sentry sentry(os); + bool ok = false; + + if (sentry) { + ok = true; + typename basic_string::size_type n = s.size(); + typename basic_string::size_type pad_len = 0; + const bool left = (os.flags() & std::ios::left) != 0; + const std::size_t w = os.width(0); + std::basic_streambuf* buf = os.rdbuf(); + + if (w != 0 && n < w) + pad_len = w - n; + + if (!left) + ok = container_detail::string_fill(os, buf, pad_len); + + ok = ok && + buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n); + + if (left) + ok = ok && container_detail::string_fill(os, buf, pad_len); + } + + if (!ok) + os.setstate(std::ios_base::failbit); + + return os; +} + + +template +std::basic_istream& +operator>>(std::basic_istream& is, basic_string& s) +{ + typename std::basic_istream::sentry sentry(is); + + if (sentry) { + std::basic_streambuf* buf = is.rdbuf(); + const std::ctype& ctype = std::use_facet >(is.getloc()); + + s.clear(); + std::size_t n = is.width(0); + if (n == 0) + n = static_cast(-1); + else + s.reserve(n); + + while (n-- > 0) { + typename Traits::int_type c1 = buf->sbumpc(); + + if (Traits::eq_int_type(c1, Traits::eof())) { + is.setstate(std::ios_base::eofbit); + break; + } + else { + CharT c = Traits::to_char_type(c1); + + if (ctype.is(std::ctype::space, c)) { + if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof())) + is.setstate(std::ios_base::failbit); + break; + } + else + s.push_back(c); + } + } + + // If we have read no characters, then set failbit. + if (s.size() == 0) + is.setstate(std::ios_base::failbit); + } + else + is.setstate(std::ios_base::failbit); + + return is; +} + +template +std::basic_istream& +getline(std::istream& is, basic_string& s,CharT delim) +{ + typename basic_string::size_type nread = 0; + typename std::basic_istream::sentry sentry(is, true); + if (sentry) { + std::basic_streambuf* buf = is.rdbuf(); + s.clear(); + + while (nread < s.max_size()) { + int c1 = buf->sbumpc(); + if (Traits::eq_int_type(c1, Traits::eof())) { + is.setstate(std::ios_base::eofbit); + break; + } + else { + ++nread; + CharT c = Traits::to_char_type(c1); + if (!Traits::eq(c, delim)) + s.push_back(c); + else + break; // Character is extracted but not appended. + } + } + } + if (nread == 0 || nread >= s.max_size()) + is.setstate(std::ios_base::failbit); + + return is; +} + +template +inline std::basic_istream& +getline(std::basic_istream& is, basic_string& s) +{ + return getline(is, s, '\n'); +} + +template +inline std::size_t hash_value(basic_string, Allocator> const& v) +{ + return hash_range(v.begin(), v.end()); +} + +}} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { + +template +struct has_trivial_destructor_after_move; + +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public ::boost::has_trivial_destructor_after_move +{}; + +} + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +#endif // BOOST_CONTAINER_STRING_HPP diff --git a/boost/container/throw_exception.hpp b/boost/container/throw_exception.hpp new file mode 100644 index 0000000..0276885 --- /dev/null +++ b/boost/container/throw_exception.hpp @@ -0,0 +1,166 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP +#define BOOST_CONTAINER_THROW_EXCEPTION_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#ifndef BOOST_NO_EXCEPTIONS + #include //for std exception types + #include //for std::bad_alloc +#else + #include + #include //for std::abort +#endif + +namespace boost { +namespace container { + +#if defined(BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS) + //The user must provide definitions for the following functions + + void throw_bad_alloc(); + + void throw_out_of_range(const char* str); + + void throw_length_error(const char* str); + + void throw_logic_error(const char* str); + + void throw_runtime_error(const char* str); + +#elif defined(BOOST_NO_EXCEPTIONS) + + inline void throw_bad_alloc() + { + BOOST_ASSERT(!"boost::container bad_alloc thrown"); + std::abort(); + } + + inline void throw_out_of_range(const char* str) + { + BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str); + std::abort(); + } + + inline void throw_length_error(const char* str) + { + BOOST_ASSERT_MSG(!"boost::container length_error thrown", str); + std::abort(); + } + + inline void throw_logic_error(const char* str) + { + BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str); + std::abort(); + } + + inline void throw_runtime_error(const char* str) + { + BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str); + std::abort(); + } + +#else //defined(BOOST_NO_EXCEPTIONS) + + //! Exception callback called by Boost.Container when fails to allocate the requested storage space. + //!
    + //!
  • If BOOST_NO_EXCEPTIONS is NOT defined std::bad_alloc() is thrown.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS + //! is NOT defined BOOST_ASSERT(!"boost::container bad_alloc thrown") is called + //! and std::abort() if the former returns.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined + //! the user must provide an implementation and the function should not return.
  • + //!
+ inline void throw_bad_alloc() + { + throw std::bad_alloc(); + } + + //! Exception callback called by Boost.Container to signal arguments out of range. + //!
    + //!
  • If BOOST_NO_EXCEPTIONS is NOT defined std::out_of_range(str) is thrown.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS + //! is NOT defined BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str) is called + //! and std::abort() if the former returns.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined + //! the user must provide an implementation and the function should not return.
  • + //!
+ inline void throw_out_of_range(const char* str) + { + throw std::out_of_range(str); + } + + //! Exception callback called by Boost.Container to signal errors resizing. + //!
    + //!
  • If BOOST_NO_EXCEPTIONS is NOT defined std::length_error(str) is thrown.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS + //! is NOT defined BOOST_ASSERT_MSG(!"boost::container length_error thrown", str) is called + //! and std::abort() if the former returns.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined + //! the user must provide an implementation and the function should not return.
  • + //!
+ inline void throw_length_error(const char* str) + { + throw std::length_error(str); + } + + //! Exception callback called by Boost.Container to report errors in the internal logical + //! of the program, such as violation of logical preconditions or class invariants. + //!
    + //!
  • If BOOST_NO_EXCEPTIONS is NOT defined std::logic_error(str) is thrown.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS + //! is NOT defined BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str) is called + //! and std::abort() if the former returns.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined + //! the user must provide an implementation and the function should not return.
  • + //!
+ inline void throw_logic_error(const char* str) + { + throw std::logic_error(str); + } + + //! Exception callback called by Boost.Container to report errors that can only be detected during runtime. + //!
    + //!
  • If BOOST_NO_EXCEPTIONS is NOT defined std::runtime_error(str) is thrown.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS + //! is NOT defined BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str) is called + //! and std::abort() if the former returns.
  • + //! + //!
  • If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined + //! the user must provide an implementation and the function should not return.
  • + //!
+ inline void throw_runtime_error(const char* str) + { + throw std::runtime_error(str); + } + +#endif + +}} //namespace boost { namespace container { + +#include + +#endif //#ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP diff --git a/boost/container/vector.hpp b/boost/container/vector.hpp new file mode 100644 index 0000000..765a2c6 --- /dev/null +++ b/boost/container/vector.hpp @@ -0,0 +1,2984 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2005-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/container for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP +#define BOOST_CONTAINER_CONTAINER_VECTOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +//#include //Already included by container_fwd.hpp +#include //for std::allocator +#include //for std::random_access_iterator_tag +#include //for std::pair,std::distance +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include //for std::initializer_list +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace container { + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//#define BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +namespace container_detail { + +#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +template +class vec_iterator +{ + public: + typedef std::random_access_iterator_tag iterator_category; + typedef typename boost::intrusive::pointer_traits::element_type value_type; + typedef typename boost::intrusive::pointer_traits::difference_type difference_type; + typedef typename if_c + < IsConst + , typename boost::intrusive::pointer_traits::template + rebind_pointer::type + , Pointer + >::type pointer; + typedef typename boost::intrusive::pointer_traits ptr_traits; + typedef typename ptr_traits::reference reference; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + Pointer m_ptr; + + public: + const Pointer &get_ptr() const BOOST_CONTAINER_NOEXCEPT + { return m_ptr; } + + Pointer &get_ptr() BOOST_CONTAINER_NOEXCEPT + { return m_ptr; } + + explicit vec_iterator(Pointer ptr) BOOST_CONTAINER_NOEXCEPT + : m_ptr(ptr) + {} + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + + //Constructors + vec_iterator() BOOST_CONTAINER_NOEXCEPT + : m_ptr() //Value initialization to achieve "null iterators" (N3644) + {} + + vec_iterator(vec_iterator const& other) BOOST_CONTAINER_NOEXCEPT + : m_ptr(other.get_ptr()) + {} + + //Pointer like operators + reference operator*() const BOOST_CONTAINER_NOEXCEPT + { return *m_ptr; } + + pointer operator->() const BOOST_CONTAINER_NOEXCEPT + { return ::boost::intrusive::pointer_traits::pointer_to(this->operator*()); } + + reference operator[](difference_type off) const BOOST_CONTAINER_NOEXCEPT + { return m_ptr[off]; } + + //Increment / Decrement + vec_iterator& operator++() BOOST_CONTAINER_NOEXCEPT + { ++m_ptr; return *this; } + + vec_iterator operator++(int) BOOST_CONTAINER_NOEXCEPT + { return vec_iterator(m_ptr++); } + + vec_iterator& operator--() BOOST_CONTAINER_NOEXCEPT + { --m_ptr; return *this; } + + vec_iterator operator--(int) BOOST_CONTAINER_NOEXCEPT + { return vec_iterator(m_ptr--); } + + //Arithmetic + vec_iterator& operator+=(difference_type off) BOOST_CONTAINER_NOEXCEPT + { m_ptr += off; return *this; } + + vec_iterator& operator-=(difference_type off) BOOST_CONTAINER_NOEXCEPT + { m_ptr -= off; return *this; } + + friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_CONTAINER_NOEXCEPT + { return vec_iterator(x.m_ptr+off); } + + friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_CONTAINER_NOEXCEPT + { right.m_ptr += off; return right; } + + friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_CONTAINER_NOEXCEPT + { left.m_ptr -= off; return left; } + + friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_CONTAINER_NOEXCEPT + { return left.m_ptr - right.m_ptr; } + + //Comparison operators + friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr == r.m_ptr; } + + friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr != r.m_ptr; } + + friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr < r.m_ptr; } + + friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr <= r.m_ptr; } + + friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr > r.m_ptr; } + + friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT + { return l.m_ptr >= r.m_ptr; } +}; + +} //namespace container_detail { + +template +const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator &it) BOOST_CONTAINER_NOEXCEPT +{ return it.get_ptr(); } + +template +Pointer &get_ptr(container_detail::vec_iterator &it) BOOST_CONTAINER_NOEXCEPT +{ return it.get_ptr(); } + +namespace container_detail { + +#else //ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +template< class MaybeConstPointer + , bool ElementTypeIsConst + = is_const< typename boost::intrusive::pointer_traits::element_type>::value > +struct vector_get_ptr_pointer_to_non_const +{ + typedef MaybeConstPointer const_pointer; + typedef boost::intrusive::pointer_traits pointer_traits_t; + typedef typename pointer_traits_t::element_type element_type; + typedef typename remove_const::type non_const_element_type; + typedef typename pointer_traits_t + ::template rebind_pointer::type return_type; + + static return_type get_ptr(const const_pointer &ptr) BOOST_CONTAINER_NOEXCEPT + { return boost::intrusive::pointer_traits::const_cast_from(ptr); } +}; + +template +struct vector_get_ptr_pointer_to_non_const +{ + typedef const Pointer & return_type; + static return_type get_ptr(const Pointer &ptr) BOOST_CONTAINER_NOEXCEPT + { return ptr; } +}; + +} //namespace container_detail { + +template +typename container_detail::vector_get_ptr_pointer_to_non_const::return_type + vector_iterator_get_ptr(const MaybeConstPointer &ptr) BOOST_CONTAINER_NOEXCEPT +{ + return container_detail::vector_get_ptr_pointer_to_non_const::get_ptr(ptr); +} + +namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + +struct uninitialized_size_t {}; +static const uninitialized_size_t uninitialized_size = uninitialized_size_t(); + +template +struct vector_value_traits_base +{ + static const bool trivial_dctr = boost::has_trivial_destructor::value; + static const bool trivial_dctr_after_move = ::boost::has_trivial_destructor_after_move::value; + static const bool trivial_copy = has_trivial_copy::value; + static const bool nothrow_copy = has_nothrow_copy::value || trivial_copy; + static const bool trivial_assign = has_trivial_assign::value; + static const bool nothrow_assign = has_nothrow_assign::value || trivial_assign; +}; + + +template +struct vector_value_traits + : public vector_value_traits_base +{ + typedef vector_value_traits_base base_t; + //This is the anti-exception array destructor + //to deallocate values already constructed + typedef typename container_detail::if_c + + ,container_detail::scoped_destructor_n + >::type ArrayDestructor; + //This is the anti-exception array deallocator + typedef container_detail::scoped_array_deallocator ArrayDeallocator; +}; + +//!This struct deallocates and allocated memory +template < class Allocator + , class AllocatorVersion = typename container_detail::version::type + > +struct vector_alloc_holder + : public Allocator +{ + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder) + + public: + typedef boost::container::allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::pointer pointer; + typedef typename allocator_traits_type::size_type size_type; + typedef typename allocator_traits_type::value_type value_type; + + //Constructor, does not throw + vector_alloc_holder() + BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor::value) + : Allocator(), m_start(), m_size(), m_capacity() + {} + + //Constructor, does not throw + template + explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT + : Allocator(boost::forward(a)), m_start(), m_size(), m_capacity() + {} + + //Constructor, does not throw + template + vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size) + : Allocator(boost::forward(a)) + , m_start() + , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this + , m_capacity() + { + if(initial_size){ + m_start = this->allocation_command(allocate_new, initial_size, initial_size, m_capacity, m_start).first; + } + } + + //Constructor, does not throw + vector_alloc_holder(uninitialized_size_t, size_type initial_size) + : Allocator() + , m_start() + , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this + , m_capacity() + { + if(initial_size){ + m_start = this->allocation_command + (allocate_new, initial_size, initial_size, m_capacity, m_start).first; + } + } + + vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_CONTAINER_NOEXCEPT + : Allocator(boost::move(static_cast(holder))) + , m_start(holder.m_start) + , m_size(holder.m_size) + , m_capacity(holder.m_capacity) + { + holder.m_start = pointer(); + holder.m_size = holder.m_capacity = 0; + } + + void first_allocation(size_type cap) + { + if(cap){ + m_start = this->allocation_command + (allocate_new, cap, cap, m_capacity, m_start).first; + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + } + } + + void first_allocation_same_allocator_type(size_type cap) + { this->first_allocation(cap); } + + ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT + { + if(this->m_capacity){ + this->alloc().deallocate(this->m_start, this->m_capacity); + } + } + + std::pair + allocation_command(boost::container::allocation_type command, + size_type limit_size, + size_type preferred_size, + size_type &received_size, const pointer &reuse = pointer()) + { + return allocator_version_traits::allocation_command + (this->alloc(), command, limit_size, preferred_size, received_size, reuse); + } + + size_type next_capacity(size_type additional_objects) const + { + return next_capacity_calculator + ::get( allocator_traits_type::max_size(this->alloc()) + , this->m_capacity, additional_objects ); + } + + pointer m_start; + size_type m_size; + size_type m_capacity; + + void swap(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT + { + boost::container::swap_dispatch(this->m_start, x.m_start); + boost::container::swap_dispatch(this->m_size, x.m_size); + boost::container::swap_dispatch(this->m_capacity, x.m_capacity); + } + + void move_from_empty(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT + { + //this->m_size was previously initialized + this->m_start = x.m_start; + this->m_capacity = x.m_capacity; + x.m_start = pointer(); + x.m_size = x.m_capacity = 0; + } + + Allocator &alloc() BOOST_CONTAINER_NOEXCEPT + { return *this; } + + const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT + { return *this; } + + const pointer &start() const BOOST_CONTAINER_NOEXCEPT { return m_start; } + const size_type &capacity() const BOOST_CONTAINER_NOEXCEPT { return m_capacity; } + void start(const pointer &p) BOOST_CONTAINER_NOEXCEPT { m_start = p; } + void capacity(const size_type &c) BOOST_CONTAINER_NOEXCEPT { m_capacity = c; } +}; + +//!This struct deallocates and allocated memory +template +struct vector_alloc_holder > + : public Allocator +{ + private: + BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder) + + public: + typedef boost::container::allocator_traits allocator_traits_type; + typedef typename allocator_traits_type::pointer pointer; + typedef typename allocator_traits_type::size_type size_type; + typedef typename allocator_traits_type::value_type value_type; + + template + friend struct vector_alloc_holder; + + //Constructor, does not throw + vector_alloc_holder() + BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor::value) + : Allocator(), m_size() + {} + + //Constructor, does not throw + template + explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT + : Allocator(boost::forward(a)), m_size() + {} + + //Constructor, does not throw + template + vector_alloc_holder(uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size) + : Allocator(boost::forward(a)) + , m_size(initial_size) //Size is initialized here... + { + //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor + this->first_allocation(initial_size); + } + + //Constructor, does not throw + vector_alloc_holder(uninitialized_size_t, size_type initial_size) + : Allocator() + , m_size(initial_size) //Size is initialized here... + { + //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor + this->first_allocation(initial_size); + } + + vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) + : Allocator(boost::move(static_cast(holder))) + , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this + { + ::boost::container::uninitialized_move_alloc_n + (this->alloc(), container_detail::to_raw_pointer(holder.start()), m_size, container_detail::to_raw_pointer(this->start())); + } + + template + vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder BOOST_RV_REF_END holder) + : Allocator() + , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort + { + //Different allocator type so we must check we have enough storage + const size_type n = holder.m_size; + this->first_allocation(n); + ::boost::container::uninitialized_move_alloc_n + (this->alloc(), container_detail::to_raw_pointer(holder.start()), n, container_detail::to_raw_pointer(this->start())); + } + + void first_allocation(size_type cap) + { + if(cap > Allocator::internal_capacity){ + throw_bad_alloc(); + } + } + + void first_allocation_same_allocator_type(size_type) BOOST_CONTAINER_NOEXCEPT + {} + + //Destructor + ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT + {} + + void swap(vector_alloc_holder &x) + { + this->priv_swap_members_impl(x); + } + + template + void swap(vector_alloc_holder &x) + { + if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){ + throw_bad_alloc(); + } + this->priv_swap_members_impl(x); + } + + void move_from_empty(vector_alloc_holder &) + { //Containers with version 0 allocators can't be moved without move elements one by one + throw_bad_alloc(); + } + + Allocator &alloc() BOOST_CONTAINER_NOEXCEPT + { return *this; } + + const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT + { return *this; } + + pointer start() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_storage(); } + size_type capacity() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_capacity; } + size_type m_size; + + private: + + template + void priv_swap_members_impl(vector_alloc_holder &x) + { + const std::size_t MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity; + value_type *const first_this = container_detail::to_raw_pointer(this->start()); + value_type *const first_x = container_detail::to_raw_pointer(x.start()); + + if(this->m_size < x.m_size){ + boost::container::deep_swap_alloc_n(this->alloc(), first_this, this->m_size, first_x, x.m_size); + } + else{ + boost::container::deep_swap_alloc_n(this->alloc(), first_x, x.m_size, first_this, this->m_size); + } + boost::container::swap_dispatch(this->m_size, x.m_size); + } +}; + +} //namespace container_detail { + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +//! A vector is a sequence that supports random access to elements, constant +//! time insertion and removal of elements at the end, and linear time insertion +//! and removal of elements at the beginning or in the middle. The number of +//! elements in a vector may vary dynamically; memory management is automatic. +//! +//! \tparam T The type of object that is stored in the vector +//! \tparam Allocator The allocator used for all internal memory management +template ) > +class vector +{ + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + typedef typename container_detail::version::type alloc_version; + boost::container::container_detail::vector_alloc_holder + m_holder; + typedef allocator_traits allocator_traits_type; + template + friend class vector; + + typedef typename ::boost::container::allocator_traits + ::pointer pointer_impl; + typedef container_detail::vec_iterator iterator_impl; + typedef container_detail::vec_iterator const_iterator_impl; + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + public: + ////////////////////////////////////////////// + // + // types + // + ////////////////////////////////////////////// + + typedef T value_type; + typedef typename ::boost::container::allocator_traits::pointer pointer; + typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; + typedef typename ::boost::container::allocator_traits::reference reference; + typedef typename ::boost::container::allocator_traits::const_reference const_reference; + typedef typename ::boost::container::allocator_traits::size_type size_type; + typedef typename ::boost::container::allocator_traits::difference_type difference_type; + typedef Allocator allocator_type; + typedef Allocator stored_allocator_type; + #if defined BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER + typedef BOOST_CONTAINER_IMPDEF(pointer) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator; + #else + typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; + typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; + #endif + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) reverse_iterator; + typedef BOOST_CONTAINER_IMPDEF(container_detail::reverse_iterator) const_reverse_iterator; + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + private: + BOOST_COPYABLE_AND_MOVABLE(vector) + typedef container_detail::vector_value_traits value_traits; + + typedef container_detail::integral_constant allocator_v0; + typedef container_detail::integral_constant allocator_v1; + typedef container_detail::integral_constant allocator_v2; + + typedef constant_iterator cvalue_iterator; + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + public: + ////////////////////////////////////////////// + // + // construct/copy/destroy + // + ////////////////////////////////////////////// + + //! Effects: Constructs a vector taking the allocator as parameter. + //! + //! Throws: If allocator_type's default constructor throws. + //! + //! Complexity: Constant. + vector() + BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor::value) + : m_holder() + {} + + //! Effects: Constructs a vector taking the allocator as parameter. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + explicit vector(const Allocator& a) BOOST_CONTAINER_NOEXCEPT + : m_holder(a) + {} + + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts n value initialized values. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's value initialization throws. + //! + //! Complexity: Linear to n. + explicit vector(size_type n) + : m_holder(container_detail::uninitialized_size, n) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += n != 0; + #endif + boost::container::uninitialized_value_init_alloc_n + (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start())); + } + + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts n default initialized values. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's default initialization throws. + //! + //! Complexity: Linear to n. + //! + //! Note: Non-standard extension + vector(size_type n, default_init_t) + : m_holder(container_detail::uninitialized_size, n) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += n != 0; + #endif + boost::container::uninitialized_default_init_alloc_n + (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start())); + } + + //! Effects: Constructs a vector + //! and inserts n copies of value. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + vector(size_type n, const T& value) + : m_holder(container_detail::uninitialized_size, n) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += n != 0; + #endif + boost::container::uninitialized_fill_alloc_n + (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start())); + } + + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts n copies of value. + //! + //! Throws: If allocation + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to n. + vector(size_type n, const T& value, const allocator_type& a) + : m_holder(container_detail::uninitialized_size, a, n) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += n != 0; + #endif + boost::container::uninitialized_fill_alloc_n + (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start())); + } + + //! Effects: Constructs a vector + //! and inserts a copy of the range [first, last) in the vector. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + vector(InIt first, InIt last) + : m_holder() + { this->insert(this->cend(), first, last); } + + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts a copy of the range [first, last) in the vector. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's constructor taking a dereferenced InIt throws. + //! + //! Complexity: Linear to the range [first, last). + template + vector(InIt first, InIt last, const allocator_type& a) + : m_holder(a) + { this->insert(this->cend(), first, last); } + + //! Effects: Copy constructs a vector. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocator_type's default constructor or allocation + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to the elements x contains. + vector(const vector &x) + : m_holder( container_detail::uninitialized_size + , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc()) + , x.size()) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += x.size() != 0; + #endif + ::boost::container::uninitialized_copy_alloc_n + ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start()) + , x.size(), container_detail::to_raw_pointer(this->m_holder.start())); + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.last()) in the vector + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + vector(std::initializer_list il, const allocator_type& a = allocator_type()) + : m_holder(a) + { + insert(cend(), il.begin(), il.end()); + } +#endif + + + //! Effects: Move constructor. Moves x's resources to *this. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + vector(BOOST_RV_REF(vector) x) BOOST_CONTAINER_NOEXCEPT + : m_holder(boost::move(x.m_holder)) + {} + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Move constructor. Moves x's resources to *this. + //! + //! Throws: If T's move constructor or allocation throws + //! + //! Complexity: Linear. + //! + //! Note: Non-standard extension to support static_vector + template + vector(BOOST_RV_REF_BEG vector BOOST_RV_REF_END x + , typename container_detail::enable_if_c + < container_detail::is_version::value>::type * = 0 + ) + : m_holder(boost::move(x.m_holder)) + {} + + #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Copy constructs a vector using the specified allocator. + //! + //! Postcondition: x == *this. + //! + //! Throws: If allocation + //! throws or T's copy constructor throws. + //! + //! Complexity: Linear to the elements x contains. + vector(const vector &x, const allocator_type &a) + : m_holder(container_detail::uninitialized_size, a, x.size()) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += x.size() != 0; + #endif + ::boost::container::uninitialized_copy_alloc_n_source + ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start()) + , x.size(), container_detail::to_raw_pointer(this->m_holder.start())); + } + + //! Effects: Move constructor using the specified allocator. + //! Moves x's resources to *this if a == allocator_type(). + //! Otherwise copies values from x to *this. + //! + //! Throws: If allocation or T's copy constructor throws. + //! + //! Complexity: Constant if a == x.get_allocator(), linear otherwise. + vector(BOOST_RV_REF(vector) x, const allocator_type &a) + : m_holder(container_detail::uninitialized_size, a, x.size()) + { + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + this->num_alloc += x.size() != 0; + #endif + if(x.m_holder.alloc() == a){ + this->m_holder.move_from_empty(x.m_holder); + } + else{ + const size_type n = x.size(); + this->m_holder.first_allocation_same_allocator_type(n); + ::boost::container::uninitialized_move_alloc_n_source + ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start()) + , n, container_detail::to_raw_pointer(this->m_holder.start())); + } + } + + //! Effects: Destroys the vector. All stored values are destroyed + //! and used memory is deallocated. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements. + ~vector() BOOST_CONTAINER_NOEXCEPT + { + boost::container::destroy_alloc_n + (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size); + //vector_alloc_holder deallocates the data + } + + //! Effects: Makes *this contain the same elements as x. + //! + //! Postcondition: this->size() == x.size(). *this contains a copy + //! of each of x's elements. + //! + //! Throws: If memory allocation throws or T's copy/move constructor/assignment throws. + //! + //! Complexity: Linear to the number of elements in x. + vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x) + { + if (&x != this){ + this->priv_copy_assign(x); + } + return *this; + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Make *this container contains elements from il. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + vector& operator=(std::initializer_list il) + { + assign(il.begin(), il.end()); + return *this; + } +#endif + + //! Effects: Move assignment. All x's values are transferred to *this. + //! + //! Postcondition: x.empty(). *this contains a the elements x had + //! before the function. + //! + //! Throws: If allocator_traits_type::propagate_on_container_move_assignment + //! is false and (allocation throws or value_type's move constructor throws) + //! + //! Complexity: Constant if allocator_traits_type:: + //! propagate_on_container_move_assignment is true or + //! this->get>allocator() == x.get_allocator(). Linear otherwise. + vector& operator=(BOOST_RV_REF(vector) x) + BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) + { + this->priv_move_assign(boost::move(x)); + return *this; + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + + //! Effects: Move assignment. All x's values are transferred to *this. + //! + //! Postcondition: x.empty(). *this contains a the elements x had + //! before the function. + //! + //! Throws: If move constructor/assignment of T throws or allocation throws + //! + //! Complexity: Linear. + //! + //! Note: Non-standard extension to support static_vector + template + typename container_detail::enable_if_c + < container_detail::is_version::value && + !container_detail::is_same::value + , vector& >::type + operator=(BOOST_RV_REF_BEG vector BOOST_RV_REF_END x) + { + this->priv_move_assign(boost::move(x)); + return *this; + } + + //! Effects: Copy assignment. All x's values are copied to *this. + //! + //! Postcondition: x.empty(). *this contains a the elements x had + //! before the function. + //! + //! Throws: If move constructor/assignment of T throws or allocation throws + //! + //! Complexity: Linear. + //! + //! Note: Non-standard extension to support static_vector + template + typename container_detail::enable_if_c + < container_detail::is_version::value && + !container_detail::is_same::value + , vector& >::type + operator=(const vector &x) + { + this->priv_copy_assign(x); + return *this; + } + + #endif + + //! Effects: Assigns the the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or T's copy/move constructor/assignment or + //! T's constructor/assignment from dereferencing InpIt throws. + //! + //! Complexity: Linear to n. + template + void assign(InIt first, InIt last + BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c + < !container_detail::is_convertible::value && + ( container_detail::is_input_iterator::value || + container_detail::is_same::value ) + >::type * = 0) ) + { + //Overwrite all elements we can from [first, last) + iterator cur = this->begin(); + const iterator end_it = this->end(); + for ( ; first != last && cur != end_it; ++cur, ++first){ + *cur = *first; + } + + if (first == last){ + //There are no more elements in the sequence, erase remaining + T* const end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + const size_type n = static_cast(end_pos - container_detail::iterator_to_raw_pointer(cur)); + this->priv_destroy_last_n(n); + } + else{ + //There are more elements in the range, insert the remaining ones + this->insert(this->cend(), first, last); + } + } + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing iniializer_list iterator throws. + //! + void assign(std::initializer_list il) + { + assign(il.begin(), il.end()); + } +#endif + + //! Effects: Assigns the the range [first, last) to *this. + //! + //! Throws: If memory allocation throws or T's copy/move constructor/assignment or + //! T's constructor/assignment from dereferencing InpIt throws. + //! + //! Complexity: Linear to n. + template + void assign(FwdIt first, FwdIt last + BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c + < !container_detail::is_convertible::value && + ( !container_detail::is_input_iterator::value && + !container_detail::is_same::value ) + >::type * = 0) + ) + { + //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first + //so we can't do any backwards allocation + const size_type input_sz = static_cast(std::distance(first, last)); + const size_type old_capacity = this->capacity(); + if(input_sz > old_capacity){ //If input range is too big, we need to reallocate + size_type real_cap = 0; + std::pair ret = + this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, input_sz, real_cap, this->m_holder.start()); + if(!ret.second){ //New allocation, just emplace new values + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + pointer const old_p = this->m_holder.start(); + if(old_p){ + this->priv_destroy_all(); + this->m_holder.alloc().deallocate(old_p, old_capacity); + } + this->m_holder.start(ret.first); + this->m_holder.capacity(real_cap); + this->m_holder.m_size = 0; + this->priv_uninitialized_construct_at_end(first, last); + return; + } + else{ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_fwd; + #endif + this->m_holder.capacity(real_cap); + //Forward expansion, use assignment + back deletion/construction that comes later + } + } + //Overwrite all elements we can from [first, last) + iterator cur = this->begin(); + const iterator end_it = this->end(); + for ( ; first != last && cur != end_it; ++cur, ++first){ + *cur = *first; + } + + if (first == last){ + //There are no more elements in the sequence, erase remaining + this->priv_destroy_last_n(this->size() - input_sz); + } + else{ + //Uninitialized construct at end the remaining range + this->priv_uninitialized_construct_at_end(first, last); + } + } + + //! Effects: Assigns the n copies of val to *this. + //! + //! Throws: If memory allocation throws or + //! T's copy/move constructor/assignment throws. + //! + //! Complexity: Linear to n. + void assign(size_type n, const value_type& val) + { this->assign(cvalue_iterator(val, n), cvalue_iterator()); } + + //! Effects: Returns a copy of the internal allocator. + //! + //! Throws: If allocator's copy constructor throws. + //! + //! Complexity: Constant. + allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.alloc(); } + + //! Effects: Returns a reference to the internal allocator. + //! + //! Throws: Nothing + //! + //! Complexity: Constant. + //! + //! Note: Non-standard extension. + const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.alloc(); } + + ////////////////////////////////////////////// + // + // iterators + // + ////////////////////////////////////////////// + + //! Effects: Returns an iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator begin() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->m_holder.start()); } + + //! Effects: Returns a const_iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator begin() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->m_holder.start()); } + + //! Effects: Returns an iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + iterator end() BOOST_CONTAINER_NOEXCEPT + { return iterator(this->m_holder.start() + this->m_holder.m_size); } + + //! Effects: Returns a const_iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator end() const BOOST_CONTAINER_NOEXCEPT + { return this->cend(); } + + //! Effects: Returns a reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->end()); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT + { return this->crbegin(); } + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT + { return reverse_iterator(this->begin()); } + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT + { return this->crend(); } + + //! Effects: Returns a const_iterator to the first element contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->m_holder.start()); } + + //! Effects: Returns a const_iterator to the end of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_iterator cend() const BOOST_CONTAINER_NOEXCEPT + { return const_iterator(this->m_holder.start() + this->m_holder.m_size); } + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->end());} + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT + { return const_reverse_iterator(this->begin()); } + + ////////////////////////////////////////////// + // + // capacity + // + ////////////////////////////////////////////// + + //! Effects: Returns true if the vector contains no elements. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + bool empty() const BOOST_CONTAINER_NOEXCEPT + { return !this->m_holder.m_size; } + + //! Effects: Returns the number of the elements contained in the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type size() const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.m_size; } + + //! Effects: Returns the largest possible size of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type max_size() const BOOST_CONTAINER_NOEXCEPT + { return allocator_traits_type::max_size(this->m_holder.alloc()); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are value initialized. + //! + //! Throws: If memory allocation throws, or T's copy/move or value initialization throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size) + { this->priv_resize(new_size, value_init); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are default initialized. + //! + //! Throws: If memory allocation throws, or T's copy/move or default initialization throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + //! + //! Note: Non-standard extension + void resize(size_type new_size, default_init_t) + { this->priv_resize(new_size, default_init); } + + //! Effects: Inserts or erases elements at the end such that + //! the size becomes n. New elements are copy constructed from x. + //! + //! Throws: If memory allocation throws, or T's copy/move constructor throws. + //! + //! Complexity: Linear to the difference between size() and new_size. + void resize(size_type new_size, const T& x) + { this->priv_resize(new_size, x); } + + //! Effects: Number of elements for which memory has been allocated. + //! capacity() is always greater than or equal to size(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + size_type capacity() const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.capacity(); } + + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws or T's copy/move constructor throws. + void reserve(size_type new_cap) + { + if (this->capacity() < new_cap){ + this->priv_reserve(new_cap, alloc_version()); + } + } + + //! Effects: Tries to deallocate the excess of memory created + //! with previous allocations. The size of the vector is unchanged + //! + //! Throws: If memory allocation throws, or T's copy/move constructor throws. + //! + //! Complexity: Linear to size(). + void shrink_to_fit() + { this->priv_shrink_to_fit(alloc_version()); } + + ////////////////////////////////////////////// + // + // element access + // + ////////////////////////////////////////////// + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference front() BOOST_CONTAINER_NOEXCEPT + { return *this->m_holder.start(); } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the first + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference front() const BOOST_CONTAINER_NOEXCEPT + { return *this->m_holder.start(); } + + //! Requires: !empty() + //! + //! Effects: Returns a reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference back() BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.start()[this->m_holder.m_size - 1]; } + + //! Requires: !empty() + //! + //! Effects: Returns a const reference to the last + //! element of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference back() const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.start()[this->m_holder.m_size - 1]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.start()[n]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT + { return this->m_holder.start()[n]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + reference at(size_type n) + { this->priv_check_range(n); return this->m_holder.start()[n]; } + + //! Requires: size() > n. + //! + //! Effects: Returns a const reference to the nth element + //! from the beginning of the container. + //! + //! Throws: std::range_error if n >= size() + //! + //! Complexity: Constant. + const_reference at(size_type n) const + { this->priv_check_range(n); return this->m_holder.start()[n]; } + + ////////////////////////////////////////////// + // + // data access + // + ////////////////////////////////////////////// + + //! Returns: Allocator pointer such that [data(),data() + size()) is a valid range. + //! For a non-empty vector, data() == &front(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + T* data() BOOST_CONTAINER_NOEXCEPT + { return container_detail::to_raw_pointer(this->m_holder.start()); } + + //! Returns: Allocator pointer such that [data(),data() + size()) is a valid range. + //! For a non-empty vector, data() == &front(). + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + const T * data() const BOOST_CONTAINER_NOEXCEPT + { return container_detail::to_raw_pointer(this->m_holder.start()); } + + ////////////////////////////////////////////// + // + // modifiers + // + ////////////////////////////////////////////// + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the end of the vector. + //! + //! Throws: If memory allocation throws or the in-place constructor throws or + //! T's copy/move constructor throws. + //! + //! Complexity: Amortized constant time. + template + void emplace_back(Args &&...args) + { + if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){ + T* const back_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + //There is more memory, just construct a new object at the end + allocator_traits_type::construct(this->m_holder.alloc(), back_pos, ::boost::forward(args)...); + ++this->m_holder.m_size; + } + else{ + typedef container_detail::insert_emplace_proxy type; + this->priv_forward_range_insert_no_capacity + (vector_iterator_get_ptr(this->cend()), 1, type(::boost::forward(args)...), alloc_version()); + } + } + + //! Requires: position must be a valid iterator of *this. + //! + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... before position + //! + //! Throws: If memory allocation throws or the in-place constructor throws or + //! T's copy/move constructor/assignment throws. + //! + //! Complexity: If position is end(), amortized constant time + //! Linear time otherwise. + template + iterator emplace(const_iterator position, Args && ...args) + { + //Just call more general insert(pos, size, value) and return iterator + typedef container_detail::insert_emplace_proxy type; + return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1 + , type(::boost::forward(args)...), alloc_version()); + } + + #else + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + T* const back_pos = container_detail::to_raw_pointer \ + (this->m_holder.start()) + this->m_holder.m_size; \ + if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){ \ + allocator_traits_type::construct (this->m_holder.alloc() \ + , back_pos BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + ++this->m_holder.m_size; \ + } \ + else{ \ + typedef container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \ + type; \ + this->priv_forward_range_insert_no_capacity \ + ( vector_iterator_get_ptr(this->cend()), 1 \ + , type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)), alloc_version()); \ + } \ + } \ + \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + iterator emplace(const_iterator pos \ + BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + typedef container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \ + type; \ + return this->priv_forward_range_insert \ + ( container_detail::to_raw_pointer(vector_iterator_get_ptr(pos)), 1 \ + , type(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)), alloc_version()); \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts a copy of x at the end of the vector. + //! + //! Throws: If memory allocation throws or + //! T's copy/move constructor throws. + //! + //! Complexity: Amortized constant time. + void push_back(const T &x); + + //! Effects: Constructs a new element in the end of the vector + //! and moves the resources of x to this new element. + //! + //! Throws: If memory allocation throws or + //! T's copy/move constructor throws. + //! + //! Complexity: Amortized constant time. + void push_back(T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back) + #endif + + #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Requires: position must be a valid iterator of *this. + //! + //! Effects: Insert a copy of x before position. + //! + //! Throws: If memory allocation throws or T's copy/move constructor/assignment throws. + //! + //! Complexity: If position is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator position, const T &x); + + //! Requires: position must be a valid iterator of *this. + //! + //! Effects: Insert a new element before position with x's resources. + //! + //! Throws: If memory allocation throws. + //! + //! Complexity: If position is end(), amortized constant time + //! Linear time otherwise. + iterator insert(const_iterator position, T &&x); + #else + BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) + #endif + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert n copies of x before pos. + //! + //! Returns: an iterator to the first inserted element or p if n is 0. + //! + //! Throws: If memory allocation throws or T's copy/move constructor throws. + //! + //! Complexity: Linear to n. + iterator insert(const_iterator p, size_type n, const T& x) + { + container_detail::insert_n_copies_proxy proxy(x); + return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy, alloc_version()); + } + + //! Requires: p must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [first, last) range before pos. + //! + //! Returns: an iterator to the first inserted element or pos if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws or T's copy/move constructor/assignment throws. + //! + //! Complexity: Linear to std::distance [first, last). + template + iterator insert(const_iterator pos, InIt first, InIt last + BOOST_CONTAINER_DOCIGN(BOOST_CONTAINER_I typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && container_detail::is_input_iterator::value + >::type * = 0) + ) + { + const size_type n_pos = pos - this->cbegin(); + iterator it(vector_iterator_get_ptr(pos)); + for(;first != last; ++first){ + it = this->emplace(it, *first); + ++it; + } + return iterator(this->m_holder.start() + n_pos); + } + + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator pos, FwdIt first, FwdIt last + , typename container_detail::enable_if_c + < !container_detail::is_convertible::value + && !container_detail::is_input_iterator::value + >::type * = 0 + ) + { + container_detail::insert_range_proxy proxy(first); + return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), std::distance(first, last), proxy, alloc_version()); + } + #endif + + //! Requires: p must be a valid iterator of *this. num, must + //! be equal to std::distance(first, last) + //! + //! Effects: Insert a copy of the [first, last) range before pos. + //! + //! Returns: an iterator to the first inserted element or pos if first == last. + //! + //! Throws: If memory allocation throws, T's constructor from a + //! dereferenced InpIt throws or T's copy/move constructor/assignment throws. + //! + //! Complexity: Linear to std::distance [first, last). + //! + //! Note: This function avoids a linear operation to calculate std::distance[first, last) + //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a + //! a non-standard extension. + #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + template + iterator insert(const_iterator pos, size_type num, InIt first, InIt last) + { + BOOST_ASSERT(container_detail::is_input_iterator::value || + num == static_cast(std::distance(first, last))); + (void)last; + container_detail::insert_range_proxy proxy(first); + return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy, alloc_version()); + } + #endif + +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: position must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before position. + //! + //! Returns: an iterator to the first inserted element or position if first == last. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + iterator insert(const_iterator position, std::initializer_list il) + { + return insert(position, il.begin(), il.end()); + } +#endif + + //! Effects: Removes the last element from the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + void pop_back() BOOST_CONTAINER_NOEXCEPT + { + //Destroy last element + this->priv_destroy_last(); + } + + //! Effects: Erases the element at position pos. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the elements between pos and the + //! last element. Constant if pos is the last element. + iterator erase(const_iterator position) + { + const pointer p = vector_iterator_get_ptr(position); + T *const pos_ptr = container_detail::to_raw_pointer(p); + T *const beg_ptr = container_detail::to_raw_pointer(this->m_holder.start()); + T *const new_end_ptr = ::boost::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr); + //Move elements forward and destroy last + this->priv_destroy_last(pos_ptr == new_end_ptr); + return iterator(p); + } + + //! Effects: Erases the elements pointed by [first, last). + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the distance between first and last + //! plus linear to the elements between pos and the last element. + iterator erase(const_iterator first, const_iterator last) + { + if (first != last){ + T* const old_end_ptr = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + T* const first_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(first)); + T* const last_ptr = container_detail::to_raw_pointer(vector_iterator_get_ptr(last)); + T* const ptr = container_detail::to_raw_pointer(boost::move(last_ptr, old_end_ptr, first_ptr)); + this->priv_destroy_last_n(old_end_ptr - ptr, last_ptr == old_end_ptr); + } + return iterator(vector_iterator_get_ptr(first)); + } + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + void swap(vector& x) BOOST_CONTAINER_NOEXCEPT_IF((!container_detail::is_version::value)) + { + //Just swap internals in case of !allocator_v0. Otherwise, deep swap + this->m_holder.swap(x.m_holder); + //And now the allocator + container_detail::bool_ flag; + container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), flag); + } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //! Effects: Swaps the contents of *this and x. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear + //! + //! Note: Non-standard extension to support static_vector + template + void swap(vector & x + , typename container_detail::enable_if_c + < container_detail::is_version::value && + !container_detail::is_same::value >::type * = 0 + ) + { this->m_holder.swap(x.m_holder); } + + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + + //! Effects: Erases all the elements of the vector. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements in the container. + void clear() BOOST_CONTAINER_NOEXCEPT + { this->priv_destroy_all(); } + + //! Effects: Returns true if x and y are equal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator==(const vector& x, const vector& y) + { + if(x.size() != y.size()){ + return false; + } + else{ + const_iterator first1(x.cbegin()), first2(y.cbegin()); + const const_iterator last1(x.cend()); + for (; first1 != last1; ++first1, ++first2) { + if (!(*first1 != *first2)) { + return false; + } + } + return true; + } + } + + //! Effects: Returns true if x and y are unequal + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator!=(const vector& x, const vector& y) + { return !(x == y); } + + //! Effects: Returns true if x is less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<(const vector& x, const vector& y) + { + const_iterator first1(x.cbegin()), first2(y.cbegin()); + const const_iterator last1(x.cend()), last2(y.cend()); + for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) { + if (*first1 < *first2) return true; + if (*first2 < *first1) return false; + } + return (first1 == last1) && (first2 != last2); + } + + //! Effects: Returns true if x is greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>(const vector& x, const vector& y) + { return y < x; } + + //! Effects: Returns true if x is equal or less than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator<=(const vector& x, const vector& y) + { return !(y < x); } + + //! Effects: Returns true if x is equal or greater than y + //! + //! Complexity: Linear to the number of elements in the container. + friend bool operator>=(const vector& x, const vector& y) + { return !(x < y); } + + //! Effects: x.swap(y) + //! + //! Complexity: Constant. + friend void swap(vector& x, vector& y) + { x.swap(y); } + + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + //! Effects: If n is less than or equal to capacity(), this call has no + //! effect. Otherwise, it is a request for allocation of additional memory + //! (memory expansion) that will not invalidate iterators. + //! If the request is successful, then capacity() is greater than or equal to + //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. + //! + //! Throws: If memory allocation allocation throws or T's copy/move constructor throws. + //! + //! Note: Non-standard extension. + bool stable_reserve(size_type new_cap) + { + const bool room_enough = this->capacity() < new_cap; + if(!room_enough && alloc_version::value < 2){ + return false; + } + else{ + //There is not enough memory, try to expand the old one + size_type real_cap = 0; + std::pair ret = this->m_holder.allocation_command + (expand_fwd, new_cap, new_cap, real_cap, this->m_holder.start()); + //Check for forward expansion + if(ret.second){ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_fwd; + #endif + this->m_holder.capacity(real_cap); + } + return ret.second; + } + } + + //Absolutely experimental. This function might change, disappear or simply crash! + template + void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it) + { + const size_type old_size_pos = this->size(); + this->reserve(old_size_pos + element_count); + T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start()); + size_type insertions_left = element_count; + size_type next_pos = old_size_pos; + size_type hole_size = element_count; + + //Exception rollback. If any copy throws before the hole is filled, values + //already inserted/copied at the end of the buffer will be destroyed. + typename value_traits::ArrayDestructor past_hole_values_destroyer + (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u)); + //Loop for each insertion backwards, first moving the elements after the insertion point, + //then inserting the element. + while(insertions_left){ + size_type pos = static_cast(*(--last_position_it)); + while(pos == size_type(-1)){ + --last_value_it; + pos = static_cast(*(--last_position_it)); + } + + BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos); + //If needed shift the range after the insertion point and the previous insertion point. + //Function will take care if the shift crosses the size() boundary, using copy/move + //or uninitialized copy/move if necessary. + size_type new_hole_size = (pos != next_pos) + ? priv_insert_ordered_at_shift_range(pos, next_pos, this->size(), insertions_left) + : hole_size + ; + if(new_hole_size > 0){ + //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards + past_hole_values_destroyer.increment_size_backwards(next_pos - pos); + //Insert the new value in the hole + allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, *(--last_value_it)); + --new_hole_size; + if(new_hole_size == 0){ + //Hole was just filled, disable exception rollback and change vector size + past_hole_values_destroyer.release(); + this->m_holder.m_size += element_count; + } + else{ + //The hole was reduced by the new insertion by one + past_hole_values_destroyer.increment_size_backwards(size_type(1u)); + } + } + else{ + if(hole_size){ + //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size + past_hole_values_destroyer.release(); + this->m_holder.m_size += element_count; + } + //Insert the new value in the already constructed range + begin_ptr[pos + insertions_left - 1] = *(--last_value_it); + } + --insertions_left; + hole_size = new_hole_size; + next_pos = pos; + } + } + + #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) + //! Effects: Inserts an object of type T constructed with + //! std::forward(args)... in the end of the vector. + //! + //! Throws: If memory allocation throws or the in-place constructor throws or + //! T's copy/move constructor throws. + //! + //! Complexity: Amortized constant time. + template + bool stable_emplace_back(Args &&...args) + { + const bool room_enough = this->m_holder.m_size < this->m_holder.capacity(); + if (BOOST_LIKELY(room_enough)){ + T* const back_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + //There is more memory, just construct a new object at the end + allocator_traits_type::construct(this->m_holder.alloc(), back_pos, ::boost::forward(args)...); + ++this->m_holder.m_size; + } + return room_enough; + } + + #else + + #define BOOST_PP_LOCAL_MACRO(n) \ + BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ + bool stable_emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ + { \ + const bool room_enough = this->m_holder.m_size < this->m_holder.capacity(); \ + if (BOOST_LIKELY(room_enough)){ \ + T* const back_pos = container_detail::to_raw_pointer \ + (this->m_holder.start()) + this->m_holder.m_size; \ + allocator_traits_type::construct (this->m_holder.alloc() \ + , back_pos BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \ + ++this->m_holder.m_size; \ + } \ + return room_enough; \ + } \ + //! + #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) + #include BOOST_PP_LOCAL_ITERATE() + + #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING + + private: + + template + void priv_move_assign(BOOST_RV_REF_BEG vector BOOST_RV_REF_END x + , typename container_detail::enable_if_c + < container_detail::is_version::value >::type * = 0) + { + if(!container_detail::is_same::value && + this->capacity() < x.size()){ + throw_bad_alloc(); + } + T* const this_start = container_detail::to_raw_pointer(m_holder.start()); + T* const other_start = container_detail::to_raw_pointer(x.m_holder.start()); + const size_type this_sz = m_holder.m_size; + const size_type other_sz = static_cast(x.m_holder.m_size); + boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz); + this->m_holder.m_size = other_sz; + } + + template + void priv_move_assign(BOOST_RV_REF_BEG vector BOOST_RV_REF_END x + , typename container_detail::enable_if_c + < !container_detail::is_version::value && + container_detail::is_same::value>::type * = 0) + { + //for move constructor, no aliasing (&x != this) is assummed. + BOOST_ASSERT(this != &x); + allocator_type &this_alloc = this->m_holder.alloc(); + allocator_type &x_alloc = x.m_holder.alloc(); + const bool propagate_alloc = allocator_traits_type:: + propagate_on_container_move_assignment::value; + container_detail::bool_ flag; + const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; + //Resources can be transferred if both allocators are + //going to be equal after this function (either propagated or already equal) + if(propagate_alloc || allocators_equal){ + //Destroy objects but retain memory in case x reuses it in the future + this->clear(); + //Move allocator if needed + container_detail::move_alloc(this_alloc, x_alloc, flag); + //Nothrow swap + this->m_holder.swap(x.m_holder); + } + //Else do a one by one move + else{ + this->assign( boost::make_move_iterator(x.begin()) + , boost::make_move_iterator(x.end())); + } + } + + template + void priv_copy_assign(const vector &x + , typename container_detail::enable_if_c + < container_detail::is_version::value >::type * = 0) + { + if(!container_detail::is_same::value && + this->capacity() < x.size()){ + throw_bad_alloc(); + } + T* const this_start = container_detail::to_raw_pointer(m_holder.start()); + T* const other_start = container_detail::to_raw_pointer(x.m_holder.start()); + const size_type this_sz = m_holder.m_size; + const size_type other_sz = static_cast(x.m_holder.m_size); + boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz); + this->m_holder.m_size = other_sz; + } + + template + void priv_copy_assign(const vector &x + , typename container_detail::enable_if_c + < !container_detail::is_version::value && + container_detail::is_same::value >::type * = 0) + { + allocator_type &this_alloc = this->m_holder.alloc(); + const allocator_type &x_alloc = x.m_holder.alloc(); + container_detail::bool_ flag; + if(flag && this_alloc != x_alloc){ + this->clear(); + this->shrink_to_fit(); + } + container_detail::assign_alloc(this_alloc, x_alloc, flag); + this->assign( container_detail::to_raw_pointer(x.m_holder.start()) + , container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size)); + } + + void priv_reserve(size_type, allocator_v0) + { throw_bad_alloc(); } + + container_detail::insert_range_proxy, T*> priv_dummy_empty_proxy() + { + return container_detail::insert_range_proxy, T*> + (::boost::make_move_iterator((T *)0)); + } + + void priv_reserve(size_type new_cap, allocator_v1) + { + //There is not enough memory, allocate a new buffer + pointer p = this->m_holder.allocate(new_cap); + //We will reuse insert code, so create a dummy input iterator + this->priv_forward_range_insert_new_allocation + ( container_detail::to_raw_pointer(p), new_cap + , container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size + , 0, this->priv_dummy_empty_proxy()); + } + + void priv_reserve(size_type new_cap, allocator_v2) + { + //There is not enough memory, allocate a new + //buffer or expand the old one. + bool same_buffer_start; + size_type real_cap = 0; + std::pair ret = this->m_holder.allocation_command + (allocate_new | expand_fwd | expand_bwd, new_cap, new_cap, real_cap, this->m_holder.start()); + + //Check for forward expansion + same_buffer_start = ret.second && this->m_holder.start() == ret.first; + if(same_buffer_start){ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_fwd; + #endif + this->m_holder.capacity(real_cap); + } + else{ //If there is no forward expansion, move objects, we will reuse insertion code + T * const new_mem = container_detail::to_raw_pointer(ret.first); + T * const ins_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + if(ret.second){ //Backwards (and possibly forward) expansion + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_bwd; + #endif + this->priv_forward_range_insert_expand_backwards + ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy()); + } + else{ //New buffer + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + this->priv_forward_range_insert_new_allocation + ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy()); + } + } + } + + void priv_destroy_last() BOOST_CONTAINER_NOEXCEPT + { + if(!value_traits::trivial_dctr){ + value_type* const p = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size - 1; + allocator_traits_type::destroy(this->get_stored_allocator(), p); + } + --this->m_holder.m_size; + } + + void priv_destroy_last(const bool moved) BOOST_CONTAINER_NOEXCEPT + { + (void)moved; + if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){ + value_type* const p = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size - 1; + allocator_traits_type::destroy(this->get_stored_allocator(), p); + } + --this->m_holder.m_size; + } + + void priv_destroy_last_n(const size_type n) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(n <= this->m_holder.m_size); + if(!value_traits::trivial_dctr){ + T* const destroy_pos = container_detail::to_raw_pointer(this->m_holder.start()) + (this->m_holder.m_size-n); + boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n); + } + this->m_holder.m_size -= n; + } + + void priv_destroy_last_n(const size_type n, const bool moved) BOOST_CONTAINER_NOEXCEPT + { + BOOST_ASSERT(n <= this->m_holder.m_size); + (void)moved; + if(!(value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved))){ + T* const destroy_pos = container_detail::to_raw_pointer(this->m_holder.start()) + (this->m_holder.m_size-n); + boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n); + } + this->m_holder.m_size -= n; + } + + template + void priv_uninitialized_construct_at_end(InpIt first, InpIt last) + { + T* const old_end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos); + this->m_holder.m_size += new_end_pos - old_end_pos; + } + + void priv_destroy_all() BOOST_CONTAINER_NOEXCEPT + { + boost::container::destroy_alloc_n + (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size); + this->m_holder.m_size = 0; + } + + template + iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x) + { + return this->priv_forward_range_insert + ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy + (::boost::forward(x)), alloc_version()); + } + + container_detail::insert_copy_proxy priv_single_insert_proxy(const T &x) + { return container_detail::insert_copy_proxy (x); } + + container_detail::insert_move_proxy priv_single_insert_proxy(BOOST_RV_REF(T) x) + { return container_detail::insert_move_proxy (x); } + + template + void priv_push_back(BOOST_FWD_REF(U) u) + { + if (BOOST_LIKELY(this->m_holder.m_size < this->m_holder.capacity())){ + //There is more memory, just construct a new object at the end + allocator_traits_type::construct + ( this->m_holder.alloc() + , container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size) + , ::boost::forward(u) ); + ++this->m_holder.m_size; + } + else{ + this->priv_forward_range_insert_no_capacity + ( vector_iterator_get_ptr(this->cend()), 1 + , this->priv_single_insert_proxy(::boost::forward(u)), alloc_version()); + } + } + + container_detail::insert_n_copies_proxy priv_resize_proxy(const T &x) + { return container_detail::insert_n_copies_proxy(x); } + + container_detail::insert_default_initialized_n_proxy priv_resize_proxy(default_init_t) + { return container_detail::insert_default_initialized_n_proxy(); } + + container_detail::insert_value_initialized_n_proxy priv_resize_proxy(value_init_t) + { return container_detail::insert_value_initialized_n_proxy(); } + + template + void priv_resize(size_type new_size, const U& u) + { + const size_type sz = this->size(); + if (new_size < sz){ + //Destroy last elements + this->priv_destroy_last_n(sz - new_size); + } + else{ + const size_type n = new_size - this->size(); + this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version()); + } + } + + void priv_shrink_to_fit(allocator_v0) BOOST_CONTAINER_NOEXCEPT + {} + + void priv_shrink_to_fit(allocator_v1) + { + const size_type cp = this->m_holder.capacity(); + if(cp){ + const size_type sz = this->size(); + if(!sz){ + this->m_holder.alloc().deallocate(this->m_holder.m_start, cp); + this->m_holder.m_start = pointer(); + this->m_holder.m_capacity = 0; + } + else if(sz < cp){ + //Allocate a new buffer. + pointer p = this->m_holder.allocate(sz); + + //We will reuse insert code, so create a dummy input iterator + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + this->priv_forward_range_insert_new_allocation + ( container_detail::to_raw_pointer(p), sz + , container_detail::to_raw_pointer(this->m_holder.start()) + , 0, this->priv_dummy_empty_proxy()); + } + } + } + + void priv_shrink_to_fit(allocator_v2) BOOST_CONTAINER_NOEXCEPT + { + const size_type cp = this->m_holder.capacity(); + if(cp){ + const size_type sz = this->size(); + if(!sz){ + this->m_holder.alloc().deallocate(this->m_holder.m_start, cp); + this->m_holder.m_start = pointer(); + this->m_holder.m_capacity = 0; + } + else{ + size_type received_size; + if(this->m_holder.allocation_command + ( shrink_in_place | nothrow_allocation + , cp, sz, received_size, this->m_holder.start()).first){ + this->m_holder.capacity(received_size); + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_shrink; + #endif + } + } + } + } + + template + iterator priv_forward_range_insert_no_capacity + (const pointer &pos, const size_type, const InsertionProxy , allocator_v0) + { + throw_bad_alloc(); + return iterator(pos); + } + + template + iterator priv_forward_range_insert_no_capacity + (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1) + { + //Check if we have enough memory or try to expand current memory + const size_type n_pos = pos - this->m_holder.start(); + T *const raw_pos = container_detail::to_raw_pointer(pos); + + const size_type new_cap = this->m_holder.next_capacity(n); + T * new_buf = container_detail::to_raw_pointer(this->m_holder.alloc().allocate(new_cap)); + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + this->priv_forward_range_insert_new_allocation + ( new_buf, new_cap, raw_pos, n, insert_range_proxy); + return iterator(this->m_holder.start() + n_pos); + } + + template + iterator priv_forward_range_insert_no_capacity + (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2) + { + //Check if we have enough memory or try to expand current memory + T *const raw_pos = container_detail::to_raw_pointer(pos); + const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start()); + + size_type real_cap = 0; + //There is not enough memory, allocate a new + //buffer or expand the old one. + std::pair ret = (this->m_holder.allocation_command + (allocate_new | expand_fwd | expand_bwd, + this->m_holder.m_size + n, this->m_holder.next_capacity(n), real_cap, this->m_holder.start())); + + //Buffer reallocated + if(ret.second){ + //Forward expansion, delay insertion + if(this->m_holder.start() == ret.first){ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_fwd; + #endif + this->m_holder.capacity(real_cap); + //Expand forward + this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy); + } + //Backwards (and possibly forward) expansion + else{ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_expand_bwd; + #endif + this->priv_forward_range_insert_expand_backwards + ( container_detail::to_raw_pointer(ret.first) + , real_cap, raw_pos, n, insert_range_proxy); + } + } + //New buffer + else{ + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + ++this->num_alloc; + #endif + this->priv_forward_range_insert_new_allocation + ( container_detail::to_raw_pointer(ret.first) + , real_cap, raw_pos, n, insert_range_proxy); + } + + return iterator(this->m_holder.start() + n_pos); + } + + template + iterator priv_forward_range_insert + (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v0) + { + //Check if we have enough memory or try to expand current memory + const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size; + + if (n > remaining){ + //This will trigger an error + throw_bad_alloc(); + } + const size_type n_pos = pos - this->m_holder.start(); + T *const raw_pos = container_detail::to_raw_pointer(pos); + this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy); + return iterator(this->m_holder.start() + n_pos); + } + + template + iterator priv_forward_range_insert + (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1) + { + //Check if we have enough memory or try to expand current memory + const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size; + T *const raw_pos = container_detail::to_raw_pointer(pos); + + if (n <= remaining){ + const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start()); + this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy); + return iterator(this->m_holder.start() + n_pos); + } + else{ + return this->priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version()); + } + } + + template + iterator priv_forward_range_insert + (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2) + { + BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size); + //Check if we have enough memory or try to expand current memory + const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size; + + bool same_buffer_start = n <= remaining; + if (!same_buffer_start){ + return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version()); + } + else{ + //Expand forward + T *const raw_pos = container_detail::to_raw_pointer(pos); + const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start()); + this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy); + return iterator(this->m_holder.start() + n_pos); + } + } + + template + iterator priv_forward_range_insert_at_end + (const size_type n, const InsertionProxy insert_range_proxy, allocator_v0) + { + //Check if we have enough memory or try to expand current memory + const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size; + + if (n > remaining){ + //This will trigger an error + throw_bad_alloc(); + } + this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy); + return this->end(); + } + + template + iterator priv_forward_range_insert_at_end + (const size_type n, const InsertionProxy insert_range_proxy, allocator_v1) + { + return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v1()); + } + + template + iterator priv_forward_range_insert_at_end + (const size_type n, const InsertionProxy insert_range_proxy, allocator_v2) + { + return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v2()); + } + + //Absolutely experimental. This function might change, disappear or simply crash! + template + void priv_insert_ordered_at( size_type element_count, BiDirPosConstIt last_position_it + , bool do_skip, BiDirSkipConstIt last_skip_it, BiDirValueIt last_value_it) + { + const size_type old_size_pos = this->size(); + this->reserve(old_size_pos + element_count); + T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start()); + size_type insertions_left = element_count; + size_type next_pos = old_size_pos; + size_type hole_size = element_count; + + //Exception rollback. If any copy throws before the hole is filled, values + //already inserted/copied at the end of the buffer will be destroyed. + typename value_traits::ArrayDestructor past_hole_values_destroyer + (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u)); + //Loop for each insertion backwards, first moving the elements after the insertion point, + //then inserting the element. + while(insertions_left){ + if(do_skip){ + size_type n = *(--last_skip_it); + std::advance(last_value_it, -difference_type(n)); + } + const size_type pos = static_cast(*(--last_position_it)); + BOOST_ASSERT(pos <= old_size_pos); + //If needed shift the range after the insertion point and the previous insertion point. + //Function will take care if the shift crosses the size() boundary, using copy/move + //or uninitialized copy/move if necessary. + size_type new_hole_size = (pos != next_pos) + ? priv_insert_ordered_at_shift_range(pos, next_pos, this->size(), insertions_left) + : hole_size + ; + if(new_hole_size > 0){ + //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards + past_hole_values_destroyer.increment_size_backwards(next_pos - pos); + //Insert the new value in the hole + allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, *(--last_value_it)); + --new_hole_size; + if(new_hole_size == 0){ + //Hole was just filled, disable exception rollback and change vector size + past_hole_values_destroyer.release(); + this->m_holder.m_size += element_count; + } + else{ + //The hole was reduced by the new insertion by one + past_hole_values_destroyer.increment_size_backwards(size_type(1u)); + } + } + else{ + if(hole_size){ + //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size + past_hole_values_destroyer.release(); + this->m_holder.m_size += element_count; + } + //Insert the new value in the already constructed range + begin_ptr[pos + insertions_left - 1] = *(--last_value_it); + } + --insertions_left; + hole_size = new_hole_size; + next_pos = pos; + } + } + + //Takes the range pointed by [first_pos, last_pos) and shifts it to the right + //by 'shift_count'. 'limit_pos' marks the end of constructed elements. + // + //Precondition: first_pos <= last_pos <= limit_pos + // + //The shift operation might cross limit_pos so elements to moved beyond limit_pos + //are uninitialized_moved with an allocator. Other elements are moved. + // + //The shift operation might left uninitialized elements after limit_pos + //and the number of uninitialized elements is returned by the function. + // + //Old situation: + // first_pos last_pos old_limit + // | | | + // ____________V_______V__________________V_____________ + //| prefix | range | suffix |raw_mem ~ + //|____________|_______|__________________|_____________~ + // + //New situation in Case Allocator (hole_size == 0): + // range is moved through move assignments + // + // first_pos last_pos limit_pos + // | | | + // ____________V_______V__________________V_____________ + //| prefix' | | | range |suffix'|raw_mem ~ + //|________________+______|___^___|_______|_____________~ + // | | + // |_>_>_>_>_>^ + // + // + //New situation in Case B (hole_size > 0): + // range is moved through uninitialized moves + // + // first_pos last_pos limit_pos + // | | | + // ____________V_______V__________________V________________ + //| prefix' | | | [hole] | range | + //|_______________________________________|________|___^___| + // | | + // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^ + // + //New situation in Case C (hole_size == 0): + // range is moved through move assignments and uninitialized moves + // + // first_pos last_pos limit_pos + // | | | + // ____________V_______V__________________V___ + //| prefix' | | | range | + //|___________________________________|___^___| + // | | + // |_>_>_>_>_>_>_>_>_>_>_>^ + size_type priv_insert_ordered_at_shift_range + (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count) + { + BOOST_ASSERT(first_pos <= last_pos); + BOOST_ASSERT(last_pos <= limit_pos); + // + T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start()); + T* const first_ptr = begin_ptr + first_pos; + T* const last_ptr = begin_ptr + last_pos; + + size_type hole_size = 0; + //Case A: + if((last_pos + shift_count) <= limit_pos){ + //All move assigned + boost::move_backward(first_ptr, last_ptr, last_ptr + shift_count); + } + //Case B: + else if((first_pos + shift_count) >= limit_pos){ + //All uninitialized_moved + ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count); + hole_size = last_pos + shift_count - limit_pos; + } + //Case C: + else{ + //Some uninitialized_moved + T* const limit_ptr = begin_ptr + limit_pos; + T* const boundary_ptr = limit_ptr - shift_count; + ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr); + //The rest is move assigned + boost::move_backward(first_ptr, boundary_ptr, limit_ptr); + } + return hole_size; + } + + private: + template + void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy) + { + T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n); + this->m_holder.m_size += n; + } + + template + void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy) + { + //n can't be 0, because there is nothing to do in that case + if(!n) return; + //There is enough memory + T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size; + const size_type elems_after = old_finish - pos; + + if (!elems_after){ + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n); + this->m_holder.m_size += n; + } + else if (elems_after >= n){ + //New elements can be just copied. + //Move to uninitialized memory last objects + ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), old_finish - n, old_finish, old_finish); + this->m_holder.m_size += n; + //Copy previous to last objects to the initialized end + boost::move_backward(pos, old_finish - n, old_finish); + //Insert new objects in the pos + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n); + } + else { + //The new elements don't fit in the [pos, end()) range. + + //Copy old [pos, end()) elements to the uninitialized memory (a gap is created) + ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n); + BOOST_TRY{ + //Copy first new elements in pos (gap is still there) + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after); + //Copy to the beginning of the unallocated zone the last new elements (the gap is closed). + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after); + this->m_holder.m_size += n; + } + BOOST_CATCH(...){ + boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after); + BOOST_RETHROW + } + BOOST_CATCH_END + } + } + + template + void priv_forward_range_insert_new_allocation + (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy) + { + //n can be zero, if we want to reallocate! + T *new_finish = new_start; + T *old_finish; + //Anti-exception rollbacks + typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap); + typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u); + + //Initialize with [begin(), pos) old buffer + //the start of the new buffer + T * const old_buffer = container_detail::to_raw_pointer(this->m_holder.start()); + if(old_buffer){ + new_finish = ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), container_detail::to_raw_pointer(this->m_holder.start()), pos, old_finish = new_finish); + new_values_destroyer.increment_size(new_finish - old_finish); + } + //Initialize new objects, starting from previous point + old_finish = new_finish; + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n); + new_finish += n; + new_values_destroyer.increment_size(new_finish - old_finish); + //Initialize from the rest of the old buffer, + //starting from previous point + if(old_buffer){ + new_finish = ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish); + //Destroy and deallocate old elements + //If there is allocated memory, destroy and deallocate + if(!value_traits::trivial_dctr_after_move) + boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size); + this->m_holder.alloc().deallocate(this->m_holder.start(), this->m_holder.capacity()); + } + this->m_holder.start(new_start); + this->m_holder.m_size = new_finish - new_start; + this->m_holder.capacity(new_cap); + //All construction successful, disable rollbacks + new_values_destroyer.release(); + new_buffer_deallocator.release(); + } + + template + void priv_forward_range_insert_expand_backwards + (T* const new_start, const size_type new_capacity, + T* const pos, const size_type n, InsertionProxy insert_range_proxy) + { + //n can be zero to just expand capacity + //Backup old data + T* const old_start = container_detail::to_raw_pointer(this->m_holder.start()); + const size_type old_size = this->m_holder.m_size; + T* const old_finish = old_start + old_size; + + //We can have 8 possibilities: + const size_type elemsbefore = static_cast(pos - old_start); + const size_type s_before = static_cast(old_start - new_start); + const size_type before_plus_new = elemsbefore + n; + + //Update the vector buffer information to a safe state + this->m_holder.start(new_start); + this->m_holder.capacity(new_capacity); + this->m_holder.m_size = 0; + + //If anything goes wrong, this object will destroy + //all the old objects to fulfill previous vector state + typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size); + //Check if s_before is big enough to hold the beginning of old data + new data + if(s_before >= before_plus_new){ + //Copy first old values before pos, after that the new objects + T *const new_elem_pos = + ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start); + this->m_holder.m_size = elemsbefore; + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n); + this->m_holder.m_size = before_plus_new; + const size_type new_size = old_size + n; + //Check if s_before is so big that even copying the old data + new data + //there is a gap between the new data and the old data + if(s_before >= new_size){ + //Old situation: + // _________________________________________________________ + //| raw_mem | old_begin | old_end | + //| __________________________________|___________|_________| + // + //New situation: + // _________________________________________________________ + //| old_begin | new | old_end | raw_mem | + //|___________|__________|_________|________________________| + // + //Now initialize the rest of memory with the last old values + if(before_plus_new != new_size){ //Special case to avoid operations in back insertion + ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new); + //All new elements correctly constructed, avoid new element destruction + this->m_holder.m_size = new_size; + } + //Old values destroyed automatically with "old_values_destroyer" + //when "old_values_destroyer" goes out of scope unless the have trivial + //destructor after move. + if(value_traits::trivial_dctr_after_move) + old_values_destroyer.release(); + } + //s_before is so big that divides old_end + else{ + //Old situation: + // __________________________________________________ + //| raw_mem | old_begin | old_end | + //| ___________________________|___________|_________| + // + //New situation: + // __________________________________________________ + //| old_begin | new | old_end | raw_mem | + //|___________|__________|_________|_________________| + // + //Now initialize the rest of memory with the last old values + //All new elements correctly constructed, avoid new element destruction + const size_type raw_gap = s_before - before_plus_new; + if(!value_traits::trivial_dctr){ + //Now initialize the rest of s_before memory with the + //first of elements after new values + ::boost::container::uninitialized_move_alloc_n + (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new); + //Now we have a contiguous buffer so program trailing element destruction + //and update size to the final size. + old_values_destroyer.shrink_forward(new_size-s_before); + this->m_holder.m_size = new_size; + //Now move remaining last objects in the old buffer begin + ::boost::move(pos + raw_gap, old_finish, old_start); + //Once moved, avoid calling the destructors if trivial after move + if(value_traits::trivial_dctr_after_move){ + old_values_destroyer.release(); + } + } + else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy + ::boost::container::uninitialized_move_alloc_n + (this->m_holder.alloc(), pos, old_finish - pos, new_start + before_plus_new); + this->m_holder.m_size = new_size; + old_values_destroyer.release(); + } + } + } + else{ + //Check if we have to do the insertion in two phases + //since maybe s_before is not big enough and + //the buffer was expanded both sides + // + //Old situation: + // _________________________________________________ + //| raw_mem | old_begin + old_end | raw_mem | + //|_________|_____________________|_________________| + // + //New situation with do_after: + // _________________________________________________ + //| old_begin + new + old_end | raw_mem | + //|___________________________________|_____________| + // + //New without do_after: + // _________________________________________________ + //| old_begin + new + old_end | raw_mem | + //|____________________________|____________________| + // + const bool do_after = n > s_before; + + //Now we can have two situations: the raw_mem of the + //beginning divides the old_begin, or the new elements: + if (s_before <= elemsbefore) { + //The raw memory divides the old_begin group: + // + //If we need two phase construction (do_after) + //new group is divided in new = new_beg + new_end groups + //In this phase only new_beg will be inserted + // + //Old situation: + // _________________________________________________ + //| raw_mem | old_begin | old_end | raw_mem | + //|_________|___________|_________|_________________| + // + //New situation with do_after(1): + //This is not definitive situation, the second phase + //will include + // _________________________________________________ + //| old_begin | new_beg | old_end | raw_mem | + //|___________|_________|_________|_________________| + // + //New situation without do_after: + // _________________________________________________ + //| old_begin | new | old_end | raw_mem | + //|___________|_____|_________|_____________________| + // + //Copy the first part of old_begin to raw_mem + ::boost::container::uninitialized_move_alloc_n + (this->m_holder.alloc(), old_start, s_before, new_start); + //The buffer is all constructed until old_end, + //so program trailing destruction and assign final size + //if !do_after, s_before+n otherwise. + size_type new_1st_range; + if(do_after){ + new_1st_range = s_before; + //release destroyer and update size + old_values_destroyer.release(); + } + else{ + new_1st_range = n; + if(value_traits::trivial_dctr_after_move) + old_values_destroyer.release(); + else{ + old_values_destroyer.shrink_forward(old_size - (s_before - n)); + } + } + this->m_holder.m_size = old_size + new_1st_range; + //Now copy the second part of old_begin overwriting itself + T *const next = ::boost::move(old_start + s_before, pos, old_start); + //Now copy the new_beg elements + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range); + + //If there is no after work and the last old part needs to be moved to front, do it + if(!do_after && (n != s_before)){ + //Now displace old_end elements + ::boost::move(pos, old_finish, next + new_1st_range); + } + } + else { + //If we have to expand both sides, + //we will play if the first new values so + //calculate the upper bound of new values + + //The raw memory divides the new elements + // + //If we need two phase construction (do_after) + //new group is divided in new = new_beg + new_end groups + //In this phase only new_beg will be inserted + // + //Old situation: + // _______________________________________________________ + //| raw_mem | old_begin | old_end | raw_mem | + //|_______________|___________|_________|_________________| + // + //New situation with do_after(): + // ____________________________________________________ + //| old_begin | new_beg | old_end | raw_mem | + //|___________|_______________|_________|______________| + // + //New situation without do_after: + // ______________________________________________________ + //| old_begin | new | old_end | raw_mem | + //|___________|_____|_________|__________________________| + // + //First copy whole old_begin and part of new to raw_mem + T * const new_pos = ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), old_start, pos, new_start); + this->m_holder.m_size = elemsbefore; + const size_type mid_n = s_before - elemsbefore; + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n); + //The buffer is all constructed until old_end, + //release destroyer + this->m_holder.m_size = old_size + s_before; + old_values_destroyer.release(); + + if(do_after){ + //Copy new_beg part + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore); + } + else{ + //Copy all new elements + const size_type rest_new = n - mid_n; + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new); + T* const move_start = old_start + rest_new; + //Displace old_end + T* const move_end = ::boost::move(pos, old_finish, move_start); + //Destroy remaining moved elements from old_end except if they + //have trivial destructor after being moved + size_type n_destroy = s_before - n; + if(!value_traits::trivial_dctr_after_move) + boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy); + this->m_holder.m_size -= n_destroy; + } + } + + //This is only executed if two phase construction is needed + if(do_after){ + //The raw memory divides the new elements + // + //Old situation: + // ______________________________________________________ + //| raw_mem | old_begin | old_end | raw_mem | + //|______________|___________|____________|______________| + // + //New situation with do_after(1): + // _______________________________________________________ + //| old_begin + new_beg | new_end |old_end | raw_mem | + //|__________________________|_________|________|_________| + // + //New situation with do_after(2): + // ______________________________________________________ + //| old_begin + new | old_end |raw | + //|_______________________________________|_________|____| + // + const size_type n_after = n - s_before; + const size_type elemsafter = old_size - elemsbefore; + + //We can have two situations: + if (elemsafter >= n_after){ + //The raw_mem from end will divide displaced old_end + // + //Old situation: + // ______________________________________________________ + //| raw_mem | old_begin | old_end | raw_mem | + //|______________|___________|____________|______________| + // + //New situation with do_after(1): + // _______________________________________________________ + //| old_begin + new_beg | new_end |old_end | raw_mem | + //|__________________________|_________|________|_________| + // + //First copy the part of old_end raw_mem + T* finish_n = old_finish - n_after; + ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), finish_n, old_finish, old_finish); + this->m_holder.m_size += n_after; + //Displace the rest of old_end to the new position + boost::move_backward(pos, finish_n, old_finish); + //Now overwrite with new_end + //The new_end part is [first + (n - n_after), last) + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after); + } + else { + //The raw_mem from end will divide new_end part + // + //Old situation: + // _____________________________________________________________ + //| raw_mem | old_begin | old_end | raw_mem | + //|______________|___________|____________|_____________________| + // + //New situation with do_after(2): + // _____________________________________________________________ + //| old_begin + new_beg | new_end |old_end | raw_mem | + //|__________________________|_______________|________|_________| + // + + const size_type mid_last_dist = n_after - elemsafter; + //First initialize data in raw memory + + //Copy to the old_end part to the uninitialized zone leaving a gap. + ::boost::container::uninitialized_move_alloc + (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist); + + typename value_traits::ArrayDestructor old_end_destroyer + (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos); + + //Copy the first part to the already constructed old_end zone + insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter); + //Copy the rest to the uninitialized zone filling the gap + insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist); + this->m_holder.m_size += n_after; + old_end_destroyer.release(); + } + } + } + } + + void priv_check_range(size_type n) const + { + //If n is out of range, throw an out_of_range exception + if (n >= this->size()){ + throw_out_of_range("vector::at out of range"); + } + } + + #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS + public: + unsigned int num_expand_fwd; + unsigned int num_expand_bwd; + unsigned int num_shrink; + unsigned int num_alloc; + void reset_alloc_stats() + { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; } + #endif + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED +}; + +}} + +#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +namespace boost { + +/* +//!has_trivial_destructor_after_move<> == true_type +//!specialization for optimizations +template +struct has_trivial_destructor_after_move > + : public ::boost::has_trivial_destructor_after_move +{}; +*/ +} + +//#define BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD + +#ifdef BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD + +namespace std { + +template +inline void swap(boost::container::vector& x, boost::container::vector& y) +{ x.swap(y); } + +} //namespace std { + +#endif + +#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + +#include + +#endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP diff --git a/boost/intrusive/any_hook.hpp b/boost/intrusive/any_hook.hpp new file mode 100644 index 0000000..1b53837 --- /dev/null +++ b/boost/intrusive/any_hook.hpp @@ -0,0 +1,336 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_ANY_HOOK_HPP +#define BOOST_INTRUSIVE_ANY_HOOK_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! Helper metafunction to define a \c \c any_base_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_any_base_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3 + #else + Options... + #endif + >::type packed_options; + + typedef generic_hook + < any_algorithms + , typename packed_options::tag + , packed_options::link_mode + , AnyBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Derive a class from this hook in order to store objects of that class +//! in an intrusive container. +//! +//! The hook admits the following options: \c tag<>, \c void_pointer<> and +//! \c link_mode<>. +//! +//! \c tag<> defines a tag to identify the node. +//! The same tag value can be used in different classes, but if a class is +//! derived from more than one \c any_base_hook, then each \c any_base_hook needs its +//! unique tag. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, \c safe_link). +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class any_base_hook + : public make_any_base_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + any_base_hook(); + + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_base_hook(const any_base_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_base_hook& operator=(const any_base_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a container an assertion is raised. + //! + //! Throws: Nothing. + ~any_base_hook(); + + //! Precondition: link_mode must be \c safe_link. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c container::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + #endif +}; + +//! Helper metafunction to define a \c \c any_member_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_any_member_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3 + #else + Options... + #endif + >::type packed_options; + + typedef generic_hook + < any_algorithms + , member_tag + , packed_options::link_mode + , NoBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Store this hook in a class to be inserted +//! in an intrusive container. +//! +//! The hook admits the following options: \c void_pointer<> and +//! \c link_mode<>. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link or \c safe_link). +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class any_member_hook + : public make_any_member_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + any_member_hook(); + + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_member_hook(const any_member_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_member_hook& operator=(const any_member_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a container an assertion is raised. + //! + //! Throws: Nothing. + ~any_member_hook(); + + //! Precondition: link_mode must be \c safe_link. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c container::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + #endif +}; + +/// @cond + +namespace detail{ + +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(old_proto_value_traits_base_hook, hooktags::is_base_hook) + +//!This option setter specifies that the container +//!must use the specified base hook +template class NodeTraits> +struct any_to_some_hook +{ + typedef typename BasicHook::template pack::proto_value_traits old_proto_value_traits; + + template + struct pack : public Base + { + struct proto_value_traits + { + //proto_value_traits::hooktags::is_base_hook is used by get_value_traits + //to detect base hooks, so mark it in case BasicHook has it. + struct hooktags + { + static const bool is_base_hook = old_proto_value_traits_base_hook_bool_is_true + ::value; + }; + + typedef old_proto_value_traits basic_hook_t; + static const bool is_any_hook = true; + + template + struct node_traits_from_voidptr + { typedef NodeTraits type; }; + }; + }; +}; + +} //namespace detail{ + +/// @endcond + +//!This option setter specifies that +//!any hook should behave as an slist hook +template +struct any_to_slist_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as an list hook +template +struct any_to_list_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as a set hook +template +struct any_to_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as an avl_set hook +template +struct any_to_avl_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that any +//!hook should behave as a bs_set hook +template +struct any_to_bs_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that any hook +//!should behave as an unordered set hook +template +struct any_to_unordered_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_ANY_HOOK_HPP diff --git a/boost/intrusive/avl_set.hpp b/boost/intrusive/avl_set.hpp new file mode 100644 index 0000000..58d6108 --- /dev/null +++ b/boost/intrusive/avl_set.hpp @@ -0,0 +1,950 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_AVL_SET_HPP +#define BOOST_INTRUSIVE_AVL_SET_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! The class template avl_set is an intrusive container, that mimics most of +//! the interface of std::set as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class avl_set_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_set_impl) + + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + + //! @copydoc ::boost::intrusive::avltree::avltree(const value_compare &,const value_traits &) + explicit avl_set_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::avltree::avltree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + avl_set_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(true, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::avltree::avltree(avltree &&) + avl_set_impl(BOOST_RV_REF(avl_set_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::avltree::operator=(avltree &&) + avl_set_impl& operator=(BOOST_RV_REF(avl_set_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::~avltree() + ~avl_set_impl(); + + //! @copydoc ::boost::intrusive::avltree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::avltree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::avltree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::avltree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::avltree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::avltree::begin() + reverse_iterator avlegin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_reverse_iterator avlegin() const; + + //! @copydoc ::boost::intrusive::avltree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::avltree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::avltree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::avltree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(iterator) + static avl_set_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(const_iterator) + static const avl_set_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(iterator) + static avl_set_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(const_iterator) + static const avl_set_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::avltree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::avltree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::avltree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::avltree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::avltree::swap + void swap(avl_set_impl& other); + + //! @copydoc ::boost::intrusive::avltree::clone_from + template + void clone_from(const avl_set_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::insert_unique(reference) + std::pair insert(reference value) + { return tree_type::insert_unique(value); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_unique(hint, value); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(hint, key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_unique(b, e); } + + //! @copydoc ::boost::intrusive::avltree::insert_unique_commit + iterator insert_commit(reference value, const insert_commit_data &commit_data) + { return tree_type::insert_unique_commit(value, commit_data); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::avltree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::avltree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::avltree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::clear + void clear(); + + //! @copydoc ::boost::intrusive::avltree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::count(const_reference)const + size_type count(const_reference value) const + { return static_cast(this->tree_type::find(value) != this->tree_type::cend()); } + + //! @copydoc ::boost::intrusive::avltree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const + { return static_cast(this->tree_type::find(key, comp) != this->tree_type::cend()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference) + std::pair equal_range(const_reference value) + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp) + { return this->tree_type::lower_bound_range(key, comp); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const + { return this->tree_type::lower_bound_range(key, comp); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::avltree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::avltree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::avltree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const avl_set_impl &x, const avl_set_impl &y); + +template +bool operator>(const avl_set_impl &x, const avl_set_impl &y); + +template +bool operator<=(const avl_set_impl &x, const avl_set_impl &y); + +template +bool operator>=(const avl_set_impl &x, const avl_set_impl &y); + +template +void swap(avl_set_impl &x, avl_set_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c set that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_avl_set +{ + /// @cond + typedef typename pack_options + < avltree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef avl_set_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class avl_set + : public make_avl_set::type +{ + typedef typename make_avl_set + ::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_set) + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit avl_set( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + avl_set( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + avl_set(BOOST_RV_REF(avl_set) x) + : Base(::boost::move(static_cast(x))) + {} + + avl_set& operator=(BOOST_RV_REF(avl_set) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static avl_set &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const avl_set &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avl_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const avl_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +//! The class template avl_multiset is an intrusive container, that mimics most of +//! the interface of std::_multiset as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class avl_multiset_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_multiset_impl) + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + //! @copydoc ::boost::intrusive::avltree::avltree(const value_compare &,const value_traits &) + explicit avl_multiset_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::avltree::avltree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + avl_multiset_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(false, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::avltree::avltree(avltree &&) + avl_multiset_impl(BOOST_RV_REF(avl_multiset_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::avltree::operator=(avltree &&) + avl_multiset_impl& operator=(BOOST_RV_REF(avl_multiset_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::~avltree() + ~avl_multiset_impl(); + + //! @copydoc ::boost::intrusive::avltree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::avltree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::avltree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::avltree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::avltree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::avltree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::avltree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::avltree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::avltree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::avltree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::avltree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::avltree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(iterator) + static avl_multiset_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_end_iterator(const_iterator) + static const avl_multiset_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(iterator) + static avl_multiset_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::avltree::container_from_iterator(const_iterator) + static const avl_multiset_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::avltree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::avltree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::avltree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::avltree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::avltree::swap + void swap(avl_multiset_impl& other); + + //! @copydoc ::boost::intrusive::avltree::clone_from + template + void clone_from(const avl_multiset_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::avltree::insert_equal(reference) + iterator insert(reference value) + { return tree_type::insert_equal(value); } + + //! @copydoc ::boost::intrusive::avltree::insert_equal(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_equal(hint, value); } + + //! @copydoc ::boost::intrusive::avltree::insert_equal(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_equal(b, e); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::avltree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::avltree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::avltree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::avltree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::avltree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::clear + void clear(); + + //! @copydoc ::boost::intrusive::avltree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::avltree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::avltree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::avltree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::avltree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::avltree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::avltree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::avltree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const avl_multiset_impl &x, const avl_multiset_impl &y); + +template +bool operator>(const avl_multiset_impl &x, const avl_multiset_impl &y); + +template +bool operator<=(const avl_multiset_impl &x, const avl_multiset_impl &y); + +template +bool operator>=(const avl_multiset_impl &x, const avl_multiset_impl &y); + +template +void swap(avl_multiset_impl &x, avl_multiset_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c avl_multiset that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_avl_multiset +{ + /// @cond + typedef typename pack_options + < avltree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef avl_multiset_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class avl_multiset + : public make_avl_multiset::type +{ + typedef typename make_avl_multiset::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(avl_multiset) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit avl_multiset( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + avl_multiset( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + avl_multiset(BOOST_RV_REF(avl_multiset) x) + : Base(::boost::move(static_cast(x))) + {} + + avl_multiset& operator=(BOOST_RV_REF(avl_multiset) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static avl_multiset &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const avl_multiset &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avl_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const avl_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_AVL_SET_HPP diff --git a/boost/intrusive/avl_set_hook.hpp b/boost/intrusive/avl_set_hook.hpp new file mode 100644 index 0000000..6fdc51b --- /dev/null +++ b/boost/intrusive/avl_set_hook.hpp @@ -0,0 +1,291 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_AVL_SET_HOOK_HPP +#define BOOST_INTRUSIVE_AVL_SET_HOOK_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! Helper metafunction to define a \c avl_set_base_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_avl_set_base_hook +{ + /// @cond + typedef typename pack_options + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type packed_options; + + typedef generic_hook + < avltree_algorithms > + , typename packed_options::tag + , packed_options::link_mode + , AvlTreeBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Derive a class from avl_set_base_hook in order to store objects in +//! in an avl_set/avl_multiset. avl_set_base_hook holds the data necessary to maintain +//! the avl_set/avl_multiset and provides an appropriate value_traits class for avl_set/avl_multiset. +//! +//! The hook admits the following options: \c tag<>, \c void_pointer<>, +//! \c link_mode<> and \c optimize_size<>. +//! +//! \c tag<> defines a tag to identify the node. +//! The same tag value can be used in different classes, but if a class is +//! derived from more than one \c list_base_hook, then each \c list_base_hook needs its +//! unique tag. +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, +//! \c auto_unlink or \c safe_link). +//! +//! \c optimize_size<> will tell the hook to optimize the hook for size instead +//! of speed. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class avl_set_base_hook + : public make_avl_set_base_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + avl_set_base_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + avl_set_base_hook(const avl_set_base_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + avl_set_base_hook& operator=(const avl_set_base_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~avl_set_base_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(avl_set_base_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +//! Helper metafunction to define a \c avl_set_member_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_avl_set_member_hook +{ + /// @cond + typedef typename pack_options + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type packed_options; + + typedef generic_hook + < avltree_algorithms > + , member_tag + , packed_options::link_mode + , NoBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Put a public data member avl_set_member_hook in order to store objects of this class in +//! an avl_set/avl_multiset. avl_set_member_hook holds the data necessary for maintaining the +//! avl_set/avl_multiset and provides an appropriate value_traits class for avl_set/avl_multiset. +//! +//! The hook admits the following options: \c void_pointer<>, +//! \c link_mode<> and \c optimize_size<>. +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, +//! \c auto_unlink or \c safe_link). +//! +//! \c optimize_size<> will tell the hook to optimize the hook for size instead +//! of speed. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class avl_set_member_hook + : public make_avl_set_member_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + avl_set_member_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + avl_set_member_hook(const avl_set_member_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + avl_set_member_hook& operator=(const avl_set_member_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~avl_set_member_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(avl_set_member_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_AVL_SET_HOOK_HPP diff --git a/boost/intrusive/avltree.hpp b/boost/intrusive/avltree.hpp new file mode 100644 index 0000000..d2adf24 --- /dev/null +++ b/boost/intrusive/avltree.hpp @@ -0,0 +1,549 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_AVLTREE_HPP +#define BOOST_INTRUSIVE_AVLTREE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond + +struct default_avltree_hook_applier +{ template struct apply{ typedef typename T::default_avltree_hook type; }; }; + +template<> +struct is_default_hook_tag +{ static const bool value = true; }; + +struct avltree_defaults +{ + typedef default_avltree_hook_applier proto_value_traits; + static const bool constant_time_size = true; + typedef std::size_t size_type; + typedef void compare; + typedef void header_holder_type; +}; + +/// @endcond + +//! The class template avltree is an intrusive AVL tree container, that +//! is used to construct intrusive avl_set and avl_multiset containers. +//! The no-throw guarantee holds only, if the value_compare object +//! doesn't throw. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class avltree_impl + /// @cond + : public bstree_impl + /// @endcond +{ + public: + typedef ValueTraits value_traits; + /// @cond + typedef bstree_impl< ValueTraits, VoidOrKeyComp, SizeType + , ConstantTimeSize, AvlTreeAlgorithms + , HeaderHolder> tree_type; + typedef tree_type implementation_defined; + /// @endcond + + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::key_type key_type; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = implementation_defined::constant_time_size; + /// @cond + private: + + //noncopyable + BOOST_MOVABLE_BUT_NOT_COPYABLE(avltree_impl) + + /// @endcond + + public: + + typedef typename implementation_defined::insert_commit_data insert_commit_data; + + + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit avltree_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + avltree_impl( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(unique, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + avltree_impl(BOOST_RV_REF(avltree_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + avltree_impl& operator=(BOOST_RV_REF(avltree_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~avltree_impl(); + + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static avltree_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const avltree_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static avltree_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const avltree_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(avltree_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const avltree_impl &src, Cloner cloner, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(reference) + iterator insert_equal(reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference) + iterator insert_equal(const_iterator hint, reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator) + template + void insert_equal(Iterator b, Iterator e); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(reference) + std::pair insert_unique(reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference) + iterator insert_unique(const_iterator hint, reference value); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique_commit + iterator insert_unique_commit(reference value, const insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator) + template + void insert_unique(Iterator b, Iterator e); + + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator< (const avltree_impl &x, const avltree_impl &y); + +template +bool operator==(const avltree_impl &x, const avltree_impl &y); + +template +bool operator!= (const avltree_impl &x, const avltree_impl &y); + +template +bool operator>(const avltree_impl &x, const avltree_impl &y); + +template +bool operator<=(const avltree_impl &x, const avltree_impl &y); + +template +bool operator>=(const avltree_impl &x, const avltree_impl &y); + +template +void swap(avltree_impl &x, avltree_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c avltree that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_avltree +{ + /// @cond + typedef typename pack_options + < avltree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef avltree_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class avltree + : public make_avltree::type +{ + typedef typename make_avltree + ::type Base; + BOOST_MOVABLE_BUT_NOT_COPYABLE(avltree) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + typedef typename Base::reverse_iterator reverse_iterator; + typedef typename Base::const_reverse_iterator const_reverse_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit avltree( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + avltree( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(unique, b, e, cmp, v_traits) + {} + + avltree(BOOST_RV_REF(avltree) x) + : Base(::boost::move(static_cast(x))) + {} + + avltree& operator=(BOOST_RV_REF(avltree) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static avltree &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const avltree &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avltree &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const avltree &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_AVLTREE_HPP diff --git a/boost/intrusive/avltree_algorithms.hpp b/boost/intrusive/avltree_algorithms.hpp new file mode 100644 index 0000000..e5bcfde --- /dev/null +++ b/boost/intrusive/avltree_algorithms.hpp @@ -0,0 +1,687 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Daniel K. O. 2005. +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond + +template +struct avltree_node_cloner + : private detail::ebo_functor_holder +{ + typedef typename NodeTraits::node_ptr node_ptr; + typedef detail::ebo_functor_holder base_t; + + avltree_node_cloner(F f) + : base_t(f) + {} + + node_ptr operator()(const node_ptr & p) + { + node_ptr n = base_t::get()(p); + NodeTraits::set_balance(n, NodeTraits::get_balance(p)); + return n; + } +}; + +namespace detail { + +template +struct avltree_node_checker + : public bstree_node_checker +{ + typedef bstree_node_checker base_checker_t; + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type + : public base_checker_t::return_type + { + return_type() : height(0) {} + int height; + }; + + avltree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker) + : base_checker_t(comp, extra_checker) + {} + + void operator () (const const_node_ptr& p, + const return_type& check_return_left, const return_type& check_return_right, + return_type& check_return) + { + const int height_diff = check_return_right.height - check_return_left.height; (void)height_diff; + BOOST_INTRUSIVE_INVARIANT_ASSERT( + (height_diff == -1 && node_traits::get_balance(p) == node_traits::negative()) || + (height_diff == 0 && node_traits::get_balance(p) == node_traits::zero()) || + (height_diff == 1 && node_traits::get_balance(p) == node_traits::positive()) + ); + check_return.height = 1 + + (check_return_left.height > check_return_right.height ? check_return_left.height : check_return_right.height); + base_checker_t::operator()(p, check_return_left, check_return_right, check_return); + } +}; + +} // namespace detail + +/// @endcond + +//! avltree_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the binary search tree +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! balance: The type of the balance factor +//! +//! Static functions: +//! +//! static node_ptr get_parent(const_node_ptr n); +//! +//! static void set_parent(node_ptr n, node_ptr parent); +//! +//! static node_ptr get_left(const_node_ptr n); +//! +//! static void set_left(node_ptr n, node_ptr left); +//! +//! static node_ptr get_right(const_node_ptr n); +//! +//! static void set_right(node_ptr n, node_ptr right); +//! +//! static balance get_balance(const_node_ptr n); +//! +//! static void set_balance(node_ptr n, balance b); +//! +//! static balance negative(); +//! +//! static balance zero(); +//! +//! static balance positive(); +template +class avltree_algorithms + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_algorithms + #endif +{ + public: + typedef typename NodeTraits::node node; + typedef NodeTraits node_traits; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef typename NodeTraits::balance balance; + + /// @cond + private: + typedef bstree_algorithms bstree_algo; + + /// @endcond + + public: + //! This type is the information that will be + //! filled by insert_unique_check + typedef typename bstree_algo::insert_commit_data insert_commit_data; + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&) + static node_ptr get_header(const const_node_ptr & n); + + //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node + static node_ptr begin_node(const const_node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::end_node + static node_ptr end_node(const const_node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree + static void swap_tree(const node_ptr & header1, const node_ptr & header2); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&) + static void swap_nodes(const node_ptr & node1, const node_ptr & node2) + { + if(node1 == node2) + return; + + node_ptr header1(bstree_algo::get_header(node1)), header2(bstree_algo::get_header(node2)); + swap_nodes(node1, header1, node2, header2); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&) + static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2) + { + if(node1 == node2) return; + + bstree_algo::swap_nodes(node1, header1, node2, header2); + //Swap balance + balance c = NodeTraits::get_balance(node1); + NodeTraits::set_balance(node1, NodeTraits::get_balance(node2)); + NodeTraits::set_balance(node2, c); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&) + static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node) + { + if(node_to_be_replaced == new_node) + return; + replace_node(node_to_be_replaced, bstree_algo::get_header(node_to_be_replaced), new_node); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&) + static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node) + { + bstree_algo::replace_node(node_to_be_replaced, header, new_node); + NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced)); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&) + static void unlink(const node_ptr & node) + { + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)) + x = NodeTraits::get_parent(x); + erase(x, node); + } + } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance + static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&) + static bool unique(const const_node_ptr & node); + + //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&) + static std::size_t size(const const_node_ptr & header); + + //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&) + static node_ptr next_node(const node_ptr & node); + + //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&) + static node_ptr prev_node(const node_ptr & node); + + //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&) + static void init(const node_ptr & node); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Requires: node must not be part of any tree. + //! + //! Effects: Initializes the header to represent an empty tree. + //! unique(header) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init_header(const node_ptr & header) + { + bstree_algo::init_header(header); + NodeTraits::set_balance(header, NodeTraits::zero()); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&) + static node_ptr erase(const node_ptr & header, const node_ptr & z) + { + typename bstree_algo::data_for_rebalance info; + bstree_algo::erase(header, z, info); + if(info.y != z){ + NodeTraits::set_balance(info.y, NodeTraits::get_balance(z)); + } + //Rebalance avltree + rebalance_after_erasure(header, info.x, info.x_parent); + return z; + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer) + template + static void clone + (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer) + { + avltree_node_cloner new_cloner(cloner); + bstree_algo::clone(source_header, target_header, new_cloner, disposer); + } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer) + template + static void clear_and_dispose(const node_ptr & header, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static node_ptr lower_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static node_ptr upper_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static node_ptr find + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static std::pair equal_range + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool) + template + static std::pair bounded_range + (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp + , bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) + template + static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare) + template + static node_ptr insert_equal_upper_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp) + { + bstree_algo::insert_equal_upper_bound(h, new_node, comp); + rebalance_after_insertion(h, new_node); + return new_node; + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare) + template + static node_ptr insert_equal_lower_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp) + { + bstree_algo::insert_equal_lower_bound(h, new_node, comp); + rebalance_after_insertion(h, new_node); + return new_node; + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare) + template + static node_ptr insert_equal + (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp) + { + bstree_algo::insert_equal(header, hint, new_node, comp); + rebalance_after_insertion(header, new_node); + return new_node; + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&) + static node_ptr insert_before + (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node) + { + bstree_algo::insert_before(header, pos, new_node); + rebalance_after_insertion(header, new_node); + return new_node; + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&) + static void push_back(const node_ptr & header, const node_ptr & new_node) + { + bstree_algo::push_back(header, new_node); + rebalance_after_insertion(header, new_node); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&) + static void push_front(const node_ptr & header, const node_ptr & new_node) + { + bstree_algo::push_front(header, new_node); + rebalance_after_insertion(header, new_node); + } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) + template + static std::pair insert_unique_check + (const const_node_ptr & header, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data); + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&) + template + static std::pair insert_unique_check + (const const_node_ptr & header, const node_ptr &hint, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data &) + static void insert_unique_commit + (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data) + { + bstree_algo::insert_unique_commit(header, new_value, commit_data); + rebalance_after_insertion(header, new_value); + } + + //! @copydoc ::boost::intrusive::bstree_algorithms::is_header + static bool is_header(const const_node_ptr & p) + { return NodeTraits::get_balance(p) == NodeTraits::zero() && bstree_algo::is_header(p); } + + + /// @cond + static bool verify(const node_ptr &header) + { + std::size_t height; + std::size_t count; + return verify_recursion(NodeTraits::get_parent(header), count, height); + } + + private: + + static bool verify_recursion(node_ptr n, std::size_t &count, std::size_t &height) + { + if (!n){ + count = 0; + height = 0; + return true; + } + std::size_t leftcount, rightcount; + std::size_t leftheight, rightheight; + if(!verify_recursion(NodeTraits::get_left (n), leftcount, leftheight) || + !verify_recursion(NodeTraits::get_right(n), rightcount, rightheight) ){ + return false; + } + count = 1u + leftcount + rightcount; + height = 1u + (leftheight > rightheight ? leftheight : rightheight); + + //If equal height, balance must be zero + if(rightheight == leftheight){ + if(NodeTraits::get_balance(n) != NodeTraits::zero()){ + BOOST_ASSERT(0); + return false; + } + } + //If right is taller than left, then the difference must be at least 1 and the balance positive + else if(rightheight > leftheight){ + if(rightheight - leftheight > 1 ){ + BOOST_ASSERT(0); + return false; + } + else if(NodeTraits::get_balance(n) != NodeTraits::positive()){ + BOOST_ASSERT(0); + return false; + } + } + //If left is taller than right, then the difference must be at least 1 and the balance negative + else{ + if(leftheight - rightheight > 1 ){ + BOOST_ASSERT(0); + return false; + } + else if(NodeTraits::get_balance(n) != NodeTraits::negative()){ + BOOST_ASSERT(0); + return false; + } + } + return true; + } + + static void rebalance_after_erasure(const node_ptr & header, node_ptr x, node_ptr x_parent) + { + for ( node_ptr root = NodeTraits::get_parent(header) + ; x != root + ; root = NodeTraits::get_parent(header), x_parent = NodeTraits::get_parent(x)) { + const balance x_parent_balance = NodeTraits::get_balance(x_parent); + //Don't cache x_is_leftchild or similar because x can be null and + //equal to both x_parent_left and x_parent_right + const node_ptr x_parent_left (NodeTraits::get_left(x_parent)); + const node_ptr x_parent_right(NodeTraits::get_right(x_parent)); + + if(x_parent_balance == NodeTraits::zero()){ + NodeTraits::set_balance( x_parent, x == x_parent_right ? NodeTraits::negative() : NodeTraits::positive() ); + break; // the height didn't change, let's stop here + } + else if(x_parent_balance == NodeTraits::negative()){ + if (x == x_parent_left) { ////x is left child or x and sibling are null + NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced + x = x_parent; + } + else { + // x is right child (x_parent_left is the left child) + BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_left); + if (NodeTraits::get_balance(x_parent_left) == NodeTraits::positive()) { + // x_parent_left MUST have a right child + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(x_parent_left)); + x = avl_rotate_left_right(x_parent, x_parent_left, header); + } + else { + avl_rotate_right(x_parent, x_parent_left, header); + x = x_parent_left; + } + + // if changed from negative to NodeTraits::positive(), no need to check above + if (NodeTraits::get_balance(x) == NodeTraits::positive()){ + break; + } + } + } + else if(x_parent_balance == NodeTraits::positive()){ + if (x == x_parent_right) { //x is right child or x and sibling are null + NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced + x = x_parent; + } + else { + // x is left child (x_parent_right is the right child) + BOOST_INTRUSIVE_INVARIANT_ASSERT(x_parent_right); + if (NodeTraits::get_balance(x_parent_right) == NodeTraits::negative()) { + // x_parent_right MUST have then a left child + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(x_parent_right)); + x = avl_rotate_right_left(x_parent, x_parent_right, header); + } + else { + avl_rotate_left(x_parent, x_parent_right, header); + x = x_parent_right; + } + // if changed from NodeTraits::positive() to negative, no need to check above + if (NodeTraits::get_balance(x) == NodeTraits::negative()){ + break; + } + } + } + else{ + BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached + } + } + } + + static void rebalance_after_insertion(const node_ptr & header, node_ptr x) + { + NodeTraits::set_balance(x, NodeTraits::zero()); + // Rebalance. + for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){ + node_ptr const x_parent(NodeTraits::get_parent(x)); + node_ptr const x_parent_left(NodeTraits::get_left(x_parent)); + const balance x_parent_balance = NodeTraits::get_balance(x_parent); + const bool x_is_leftchild(x == x_parent_left); + if(x_parent_balance == NodeTraits::zero()){ + // if x is left, parent will have parent->bal_factor = negative + // else, parent->bal_factor = NodeTraits::positive() + NodeTraits::set_balance( x_parent, x_is_leftchild ? NodeTraits::negative() : NodeTraits::positive() ); + x = x_parent; + } + else if(x_parent_balance == NodeTraits::positive()){ + // if x is a left child, parent->bal_factor = zero + if (x_is_leftchild) + NodeTraits::set_balance(x_parent, NodeTraits::zero()); + else{ // x is a right child, needs rebalancing + if (NodeTraits::get_balance(x) == NodeTraits::negative()) + avl_rotate_right_left(x_parent, x, header); + else + avl_rotate_left(x_parent, x, header); + } + break; + } + else if(x_parent_balance == NodeTraits::negative()){ + // if x is a left child, needs rebalancing + if (x_is_leftchild) { + if (NodeTraits::get_balance(x) == NodeTraits::positive()) + avl_rotate_left_right(x_parent, x, header); + else + avl_rotate_right(x_parent, x, header); + } + else + NodeTraits::set_balance(x_parent, NodeTraits::zero()); + break; + } + else{ + BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached + } + } + } + + static void left_right_balancing(const node_ptr & a, const node_ptr & b, const node_ptr & c) + { + // balancing... + const balance c_balance = NodeTraits::get_balance(c); + const balance zero_balance = NodeTraits::zero(); + const balance posi_balance = NodeTraits::positive(); + const balance nega_balance = NodeTraits::negative(); + NodeTraits::set_balance(c, zero_balance); + if(c_balance == nega_balance){ + NodeTraits::set_balance(a, posi_balance); + NodeTraits::set_balance(b, zero_balance); + } + else if(c_balance == zero_balance){ + NodeTraits::set_balance(a, zero_balance); + NodeTraits::set_balance(b, zero_balance); + } + else if(c_balance == posi_balance){ + NodeTraits::set_balance(a, zero_balance); + NodeTraits::set_balance(b, nega_balance); + } + else{ + BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached + } + } + + static node_ptr avl_rotate_left_right(const node_ptr a, const node_ptr a_oldleft, const node_ptr & hdr) + { // [note: 'a_oldleft' is 'b'] + // | | // + // a(-2) c // + // / \ / \ // + // / \ ==> / \ // + // (pos)b [g] b a // + // / \ / \ / \ // + // [d] c [d] e f [g] // + // / \ // + // e f // + const node_ptr c = NodeTraits::get_right(a_oldleft); + bstree_algo::rotate_left_no_parent_fix(a_oldleft, c); + //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_left(a, c)] + //as c is not root and another rotation is coming + bstree_algo::rotate_right(a, c, NodeTraits::get_parent(a), hdr); + left_right_balancing(a, a_oldleft, c); + return c; + } + + static node_ptr avl_rotate_right_left(const node_ptr a, const node_ptr a_oldright, const node_ptr & hdr) + { // [note: 'a_oldright' is 'b'] + // | | // + // a(pos) c // + // / \ / \ // + // / \ / \ // + // [d] b(neg) ==> a b // + // / \ / \ / \ // + // c [g] [d] e f [g] // + // / \ // + // e f // + const node_ptr c (NodeTraits::get_left(a_oldright)); + bstree_algo::rotate_right_no_parent_fix(a_oldright, c); + //No need to link c with a [NodeTraits::set_parent(c, a) + NodeTraits::set_right(a, c)] + //as c is not root and another rotation is coming. + bstree_algo::rotate_left(a, c, NodeTraits::get_parent(a), hdr); + left_right_balancing(a_oldright, a, c); + return c; + } + + static void avl_rotate_left(const node_ptr &x, const node_ptr &x_oldright, const node_ptr & hdr) + { + bstree_algo::rotate_left(x, x_oldright, NodeTraits::get_parent(x), hdr); + + // reset the balancing factor + if (NodeTraits::get_balance(x_oldright) == NodeTraits::positive()) { + NodeTraits::set_balance(x, NodeTraits::zero()); + NodeTraits::set_balance(x_oldright, NodeTraits::zero()); + } + else { // this doesn't happen during insertions + NodeTraits::set_balance(x, NodeTraits::positive()); + NodeTraits::set_balance(x_oldright, NodeTraits::negative()); + } + } + + static void avl_rotate_right(const node_ptr &x, const node_ptr &x_oldleft, const node_ptr & hdr) + { + bstree_algo::rotate_right(x, x_oldleft, NodeTraits::get_parent(x), hdr); + + // reset the balancing factor + if (NodeTraits::get_balance(x_oldleft) == NodeTraits::negative()) { + NodeTraits::set_balance(x, NodeTraits::zero()); + NodeTraits::set_balance(x_oldleft, NodeTraits::zero()); + } + else { // this doesn't happen during insertions + NodeTraits::set_balance(x, NodeTraits::negative()); + NodeTraits::set_balance(x_oldleft, NodeTraits::positive()); + } + } + + /// @endcond +}; + +/// @cond + +template +struct get_algo +{ + typedef avltree_algorithms type; +}; + +template +struct get_node_checker +{ + typedef detail::avltree_node_checker type; +}; + +/// @endcond + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP diff --git a/boost/intrusive/bs_set.hpp b/boost/intrusive/bs_set.hpp new file mode 100644 index 0000000..b87a543 --- /dev/null +++ b/boost/intrusive/bs_set.hpp @@ -0,0 +1,948 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2013-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_BS_SET_HPP +#define BOOST_INTRUSIVE_BS_SET_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! The class template bs_set is an intrusive container, that mimics most of +//! the interface of std::set as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bs_set_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_set_impl) + + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit bs_set_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + bs_set_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(true, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + bs_set_impl(BOOST_RV_REF(bs_set_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + bs_set_impl& operator=(BOOST_RV_REF(bs_set_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~bs_set_impl(); + + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static bs_set_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const bs_set_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static bs_set_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const bs_set_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(bs_set_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const bs_set_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::insert_unique(reference) + std::pair insert(reference value) + { return tree_type::insert_unique(value); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_unique(hint, value); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyValueCompare,insert_commit_data&) + template + std::pair insert_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { return tree_type::insert_unique_check(hint, key, key_value_comp, commit_data); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_unique(b, e); } + + //! @copydoc ::boost::intrusive::bstree::insert_unique_commit + iterator insert_commit(reference value, const insert_commit_data &commit_data) + { return tree_type::insert_unique_commit(value, commit_data); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const + { return static_cast(this->tree_type::find(value) == this->tree_type::cend()); } + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const + { return static_cast(this->tree_type::find(key, comp) == this->tree_type::cend()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + #endif // #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference) + std::pair equal_range(const_reference value) + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp) + { return this->tree_type::lower_bound_range(key, comp); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const + { return this->tree_type::lower_bound_range(value); } + + //! @copydoc ::boost::intrusive::rbtree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const + { return this->tree_type::lower_bound_range(key, comp); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator>(const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator<=(const bs_set_impl &x, const bs_set_impl &y); + +template +bool operator>=(const bs_set_impl &x, const bs_set_impl &y); + +template +void swap(bs_set_impl &x, bs_set_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c bs_set that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_set +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bs_set_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_set + : public make_bs_set::type +{ + typedef typename make_bs_set + ::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_set) + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit bs_set( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bs_set( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + bs_set(BOOST_RV_REF(bs_set) x) + : Base(::boost::move(static_cast(x))) + {} + + bs_set& operator=(BOOST_RV_REF(bs_set) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bs_set &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bs_set &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bs_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bs_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +//! The class template bs_multiset is an intrusive container, that mimics most of +//! the interface of std::multiset as described in the C++ standard. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bs_multiset_impl +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + : public bstree_impl +#endif +{ + /// @cond + typedef bstree_impl tree_type; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_multiset_impl) + typedef tree_type implementation_defined; + /// @endcond + + public: + typedef typename implementation_defined::value_type value_type; + typedef typename implementation_defined::value_traits value_traits; + typedef typename implementation_defined::pointer pointer; + typedef typename implementation_defined::const_pointer const_pointer; + typedef typename implementation_defined::reference reference; + typedef typename implementation_defined::const_reference const_reference; + typedef typename implementation_defined::difference_type difference_type; + typedef typename implementation_defined::size_type size_type; + typedef typename implementation_defined::value_compare value_compare; + typedef typename implementation_defined::key_compare key_compare; + typedef typename implementation_defined::iterator iterator; + typedef typename implementation_defined::const_iterator const_iterator; + typedef typename implementation_defined::reverse_iterator reverse_iterator; + typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator; + typedef typename implementation_defined::insert_commit_data insert_commit_data; + typedef typename implementation_defined::node_traits node_traits; + typedef typename implementation_defined::node node; + typedef typename implementation_defined::node_ptr node_ptr; + typedef typename implementation_defined::const_node_ptr const_node_ptr; + typedef typename implementation_defined::node_algorithms node_algorithms; + + static const bool constant_time_size = tree_type::constant_time_size; + + public: + //! @copydoc ::boost::intrusive::bstree::bstree(const value_compare &,const value_traits &) + explicit bs_multiset_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const value_compare &,const value_traits &) + template + bs_multiset_impl( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : tree_type(false, b, e, cmp, v_traits) + {} + + //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&) + bs_multiset_impl(BOOST_RV_REF(bs_multiset_impl) x) + : tree_type(::boost::move(static_cast(x))) + {} + + //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&) + bs_multiset_impl& operator=(BOOST_RV_REF(bs_multiset_impl) x) + { return static_cast(tree_type::operator=(::boost::move(static_cast(x)))); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::~bstree() + ~bs_multiset_impl(); + + //! @copydoc ::boost::intrusive::bstree::begin() + iterator begin(); + + //! @copydoc ::boost::intrusive::bstree::begin()const + const_iterator begin() const; + + //! @copydoc ::boost::intrusive::bstree::cbegin()const + const_iterator cbegin() const; + + //! @copydoc ::boost::intrusive::bstree::end() + iterator end(); + + //! @copydoc ::boost::intrusive::bstree::end()const + const_iterator end() const; + + //! @copydoc ::boost::intrusive::bstree::cend()const + const_iterator cend() const; + + //! @copydoc ::boost::intrusive::bstree::rbegin() + reverse_iterator rbegin(); + + //! @copydoc ::boost::intrusive::bstree::rbegin()const + const_reverse_iterator rbegin() const; + + //! @copydoc ::boost::intrusive::bstree::crbegin()const + const_reverse_iterator crbegin() const; + + //! @copydoc ::boost::intrusive::bstree::rend() + reverse_iterator rend(); + + //! @copydoc ::boost::intrusive::bstree::rend()const + const_reverse_iterator rend() const; + + //! @copydoc ::boost::intrusive::bstree::crend()const + const_reverse_iterator crend() const; + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator) + static bs_multiset_impl &container_from_end_iterator(iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator) + static const bs_multiset_impl &container_from_end_iterator(const_iterator end_iterator); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator) + static bs_multiset_impl &container_from_iterator(iterator it); + + //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator) + static const bs_multiset_impl &container_from_iterator(const_iterator it); + + //! @copydoc ::boost::intrusive::bstree::key_comp()const + key_compare key_comp() const; + + //! @copydoc ::boost::intrusive::bstree::value_comp()const + value_compare value_comp() const; + + //! @copydoc ::boost::intrusive::bstree::empty()const + bool empty() const; + + //! @copydoc ::boost::intrusive::bstree::size()const + size_type size() const; + + //! @copydoc ::boost::intrusive::bstree::swap + void swap(bs_multiset_impl& other); + + //! @copydoc ::boost::intrusive::bstree::clone_from + template + void clone_from(const bs_multiset_impl &src, Cloner cloner, Disposer disposer); + + #endif //#ifdef BOOST_iNTRUSIVE_DOXYGEN_INVOKED + + //! @copydoc ::boost::intrusive::bstree::insert_equal(reference) + iterator insert(reference value) + { return tree_type::insert_equal(value); } + + //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference) + iterator insert(const_iterator hint, reference value) + { return tree_type::insert_equal(hint, value); } + + //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator) + template + void insert(Iterator b, Iterator e) + { tree_type::insert_equal(b, e); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! @copydoc ::boost::intrusive::bstree::insert_before + iterator insert_before(const_iterator pos, reference value); + + //! @copydoc ::boost::intrusive::bstree::push_back + void push_back(reference value); + + //! @copydoc ::boost::intrusive::bstree::push_front + void push_front(reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator) + iterator erase(const_iterator i); + + //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator) + iterator erase(const_iterator b, const_iterator e); + + //! @copydoc ::boost::intrusive::bstree::erase(const_reference) + size_type erase(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyValueCompare) + template + size_type erase(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator i, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer) + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_reference, Disposer) + template + size_type erase_and_dispose(const_reference value, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyValueCompare,Disposer) + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::clear + void clear(); + + //! @copydoc ::boost::intrusive::bstree::clear_and_dispose + template + void clear_and_dispose(Disposer disposer); + + //! @copydoc ::boost::intrusive::bstree::count(const_reference)const + size_type count(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyValueCompare)const + template + size_type count(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference) + iterator lower_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare) + template + iterator lower_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const_reference)const + const_iterator lower_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyValueCompare)const + template + const_iterator lower_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference) + iterator upper_bound(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare) + template + iterator upper_bound(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const_reference)const + const_iterator upper_bound(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyValueCompare)const + template + const_iterator upper_bound(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::find(const_reference) + iterator find(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare) + template + iterator find(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::find(const_reference)const + const_iterator find(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyValueCompare)const + template + const_iterator find(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference) + std::pair equal_range(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare) + template + std::pair equal_range(const KeyType& key, KeyValueCompare comp); + + //! @copydoc ::boost::intrusive::bstree::equal_range(const_reference)const + std::pair + equal_range(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyValueCompare)const + template + std::pair + equal_range(const KeyType& key, KeyValueCompare comp) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool) + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool) + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const_reference,const_reference,bool,bool)const + std::pair + bounded_range(const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyValueCompare,bool,bool)const + template + std::pair bounded_range + (const KeyType& lower_key, const KeyType& upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference) + static iterator s_iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference) + static const_iterator s_iterator_to(const_reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(reference) + iterator iterator_to(reference value); + + //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const + const_iterator iterator_to(const_reference value) const; + + //! @copydoc ::boost::intrusive::bstree::init_node(reference) + static void init_node(reference value); + + //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance + pointer unlink_leftmost_without_rebalance(); + + //! @copydoc ::boost::intrusive::bstree::replace_node + void replace_node(iterator replace_this, reference with_this); + + //! @copydoc ::boost::intrusive::bstree::remove_node + void remove_node(reference value); + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +template +bool operator!= (const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator>(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator<=(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +bool operator>=(const bs_multiset_impl &x, const bs_multiset_impl &y); + +template +void swap(bs_multiset_impl &x, bs_multiset_impl &y); + +#endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + +//! Helper metafunction to define a \c bs_multiset that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_multiset +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bs_multiset_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_multiset + : public make_bs_multiset::type +{ + typedef typename make_bs_multiset::type Base; + + BOOST_MOVABLE_BUT_NOT_COPYABLE(bs_multiset) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + explicit bs_multiset( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bs_multiset( Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(b, e, cmp, v_traits) + {} + + bs_multiset(BOOST_RV_REF(bs_multiset) x) + : Base(::boost::move(static_cast(x))) + {} + + bs_multiset& operator=(BOOST_RV_REF(bs_multiset) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bs_multiset &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bs_multiset &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bs_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bs_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BS_SET_HPP diff --git a/boost/intrusive/bs_set_hook.hpp b/boost/intrusive/bs_set_hook.hpp new file mode 100644 index 0000000..cb5af30 --- /dev/null +++ b/boost/intrusive/bs_set_hook.hpp @@ -0,0 +1,286 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_BS_SET_HOOK_HPP +#define BOOST_INTRUSIVE_BS_SET_HOOK_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! Helper metafunction to define a \c bs_set_base_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_set_base_hook +{ + /// @cond + typedef typename pack_options + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + < hook_defaults, O1, O2, O3> + #else + < hook_defaults, Options...> + #endif + ::type packed_options; + + typedef generic_hook + < bstree_algorithms > + , typename packed_options::tag + , packed_options::link_mode + , BsTreeBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Derive a class from bs_set_base_hook in order to store objects in +//! in a bs_set/bs_multiset. bs_set_base_hook holds the data necessary to maintain +//! the bs_set/bs_multiset and provides an appropriate value_traits class for bs_set/bs_multiset. +//! +//! The hook admits the following options: \c tag<>, \c void_pointer<>, +//! \c link_mode<>. +//! +//! \c tag<> defines a tag to identify the node. +//! The same tag value can be used in different classes, but if a class is +//! derived from more than one \c list_base_hook, then each \c list_base_hook needs its +//! unique tag. +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, +//! \c auto_unlink or \c safe_link). +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_set_base_hook + : public make_bs_set_base_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type + +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + bs_set_base_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + bs_set_base_hook(const bs_set_base_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + bs_set_base_hook& operator=(const bs_set_base_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~bs_set_base_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(bs_set_base_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +//! Helper metafunction to define a \c bs_set_member_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bs_set_member_hook +{ + /// @cond + typedef typename pack_options + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + < hook_defaults, O1, O2, O3> + #else + < hook_defaults, Options...> + #endif + + ::type packed_options; + + typedef generic_hook + < bstree_algorithms > + , member_tag + , packed_options::link_mode + , NoBaseHookId + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Put a public data member bs_set_member_hook in order to store objects of this class in +//! a bs_set/bs_multiset. bs_set_member_hook holds the data necessary for maintaining the +//! bs_set/bs_multiset and provides an appropriate value_traits class for bs_set/bs_multiset. +//! +//! The hook admits the following options: \c void_pointer<>, \c link_mode<>. +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the container configured to use this hook. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, +//! \c auto_unlink or \c safe_link). +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bs_set_member_hook + : public make_bs_set_member_hook + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + + #else + + #endif + ::type +{ + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + public: + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + bs_set_member_hook(); + + //! Effects: If link_mode is \c auto_unlink or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + bs_set_member_hook(const bs_set_member_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + bs_set_member_hook& operator=(const bs_set_member_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a set an assertion is raised. If link_mode is + //! \c auto_unlink and \c is_linked() is true, the node is unlinked. + //! + //! Throws: Nothing. + ~bs_set_member_hook(); + + //! Effects: Swapping two nodes swaps the position of the elements + //! related to those nodes in one or two containers. That is, if the node + //! this is part of the element e1, the node x is part of the element e2 + //! and both elements are included in the containers s1 and s2, then after + //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1 + //! at the position of e1. If one element is not in a container, then + //! after the swap-operation the other element is not in a container. + //! Iterators to e1 and e2 related to those nodes are invalidated. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + void swap_nodes(bs_set_member_hook &other); + + //! Precondition: link_mode must be \c safe_link or \c auto_unlink. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c set::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + + //! Effects: Removes the node if it's inserted in a container. + //! This function is only allowed if link_mode is \c auto_unlink. + //! + //! Throws: Nothing. + void unlink(); + #endif +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BS_SET_HOOK_HPP diff --git a/boost/intrusive/bstree.hpp b/boost/intrusive/bstree.hpp new file mode 100644 index 0000000..44b02bb --- /dev/null +++ b/boost/intrusive/bstree.hpp @@ -0,0 +1,2177 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2013-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_BSTREE_HPP +#define BOOST_INTRUSIVE_BSTREE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include //pair,lexicographical_compare +#include //swap +#include //size_t... +#include //less, equal_to + + +namespace boost { +namespace intrusive { + +/// @cond + +struct default_bstree_hook_applier +{ template struct apply{ typedef typename T::default_bstree_hook type; }; }; + +template<> +struct is_default_hook_tag +{ static const bool value = true; }; + +struct bstree_defaults +{ + typedef default_bstree_hook_applier proto_value_traits; + static const bool constant_time_size = true; + typedef std::size_t size_type; + typedef void compare; + static const bool floating_point = true; //For sgtree + typedef void priority; //For treap + typedef void header_holder_type; +}; + +template +struct bstbase3 +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node node_type; + typedef typename get_algo::type node_algorithms; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef tree_iterator iterator; + typedef tree_iterator const_iterator; + typedef boost::intrusive::detail::reverse_iterator reverse_iterator; + typedef boost::intrusive::detail::reverse_iterator const_reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef HeaderHolder header_holder_type; + + static const bool safemode_or_autounlink = is_safe_autounlink::value; + static const bool stateful_value_traits = detail::is_stateful_value_traits::value; + static const bool has_container_from_iterator = + detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value; + + struct holder_t : public ValueTraits + { + explicit holder_t(const ValueTraits &vtraits) + : ValueTraits(vtraits) + {} + header_holder_type root; + } holder; + + static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator) + { + BOOST_STATIC_ASSERT(has_container_from_iterator); + node_ptr p = end_iterator.pointed_node(); + header_holder_type* h = header_holder_type::get_holder(p); + holder_t *holder = get_parent_from_member(h, &holder_t::root); + bstbase3 *base = get_parent_from_member (holder, &bstbase3::holder); + return *base; + } + + bstbase3(const ValueTraits &vtraits) + : holder(vtraits) + { + node_algorithms::init_header(this->header_ptr()); + } + + node_ptr header_ptr() + { return holder.root.get_node(); } + + const_node_ptr header_ptr() const + { return holder.root.get_node(); } + + const value_traits &get_value_traits() const + { return this->holder; } + + value_traits &get_value_traits() + { return this->holder; } + + typedef typename boost::intrusive::value_traits_pointers + ::const_value_traits_ptr const_value_traits_ptr; + + const_value_traits_ptr priv_value_traits_ptr() const + { return pointer_traits::pointer_to(this->get_value_traits()); } + + iterator begin() + { return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator begin() const + { return cbegin(); } + + const_iterator cbegin() const + { return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + iterator end() + { return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator end() const + { return cend(); } + + const_iterator cend() const + { return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + iterator root() + { return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + const_iterator root() const + { return croot(); } + + const_iterator croot() const + { return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); } + + reverse_iterator rbegin() + { return reverse_iterator(end()); } + + const_reverse_iterator rbegin() const + { return const_reverse_iterator(end()); } + + const_reverse_iterator crbegin() const + { return const_reverse_iterator(end()); } + + reverse_iterator rend() + { return reverse_iterator(begin()); } + + const_reverse_iterator rend() const + { return const_reverse_iterator(begin()); } + + const_reverse_iterator crend() const + { return const_reverse_iterator(begin()); } + + void replace_node(iterator replace_this, reference with_this) + { + node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this) + , this->header_ptr() + , get_value_traits().to_node_ptr(with_this)); + if(safemode_or_autounlink) + node_algorithms::init(replace_this.pointed_node()); + } + + void rebalance() + { node_algorithms::rebalance(this->header_ptr()); } + + iterator rebalance_subtree(iterator root) + { return iterator(node_algorithms::rebalance_subtree(root.pointed_node()), this->priv_value_traits_ptr()); } + + static iterator s_iterator_to(reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr()); + } + + static const_iterator s_iterator_to(const_reference value) + { + BOOST_STATIC_ASSERT((!stateful_value_traits)); + return const_iterator (value_traits::to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), const_value_traits_ptr()); + } + + iterator iterator_to(reference value) + { return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); } + + const_iterator iterator_to(const_reference value) const + { return const_iterator (this->get_value_traits().to_node_ptr(*pointer_traits::const_cast_from(pointer_traits::pointer_to(value))), this->priv_value_traits_ptr()); } + + static void init_node(reference value) + { node_algorithms::init(value_traits::to_node_ptr(value)); } + +}; + +template +struct get_less +{ + typedef Less type; +}; + +template +struct get_less +{ + typedef ::std::less type; +}; + +template +struct bstbase2 + //Put the (possibly empty) functor in the first position to get EBO in MSVC + : public detail::ebo_functor_holder::type> + , public bstbase3 +{ + typedef bstbase3 treeheader_t; + typedef typename treeheader_t::value_traits value_traits; + typedef typename treeheader_t::node_algorithms node_algorithms; + typedef typename get_less + < VoidOrKeyComp, typename value_traits::value_type>::type value_compare; + typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; + typedef typename treeheader_t::iterator iterator; + typedef typename treeheader_t::const_iterator const_iterator; + typedef typename treeheader_t::node_ptr node_ptr; + typedef typename treeheader_t::const_node_ptr const_node_ptr; + + bstbase2(const value_compare &comp, const ValueTraits &vtraits) + : detail::ebo_functor_holder(comp), treeheader_t(vtraits) + {} + + const value_compare &comp() const + { return this->get(); } + + value_compare &comp() + { return this->get(); } + + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef typename node_algorithms::insert_commit_data insert_commit_data; + + value_compare value_comp() const + { return this->comp(); } + + key_compare key_comp() const + { return this->comp(); } + + //lower_bound + iterator lower_bound(const_reference value) + { return this->lower_bound(value, this->comp()); } + + const_iterator lower_bound(const_reference value) const + { return this->lower_bound(value, this->comp()); } + + template + iterator lower_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator(node_algorithms::lower_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + template + const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator(node_algorithms::lower_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //upper_bound + iterator upper_bound(const_reference value) + { return this->upper_bound(value, this->comp()); } + + template + iterator upper_bound(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator(node_algorithms::upper_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + const_iterator upper_bound(const_reference value) const + { return this->upper_bound(value, this->comp()); } + + template + const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator(node_algorithms::upper_bound + (this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //find + iterator find(const_reference value) + { return this->find(value, this->comp()); } + + template + iterator find(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return iterator + (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + const_iterator find(const_reference value) const + { return this->find(value, this->comp()); } + + template + const_iterator find(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + return const_iterator + (node_algorithms::find(this->header_ptr(), key, key_node_comp), this->priv_value_traits_ptr()); + } + + //equal_range + std::pair equal_range(const_reference value) + { return this->equal_range(value, this->comp()); } + + template + std::pair equal_range(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair + equal_range(const_reference value) const + { return this->equal_range(value, this->comp()); } + + template + std::pair + equal_range(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::equal_range(this->header_ptr(), key, key_node_comp)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //lower_bound_range + std::pair lower_bound_range(const_reference value) + { return this->lower_bound_range(value, this->comp()); } + + template + std::pair lower_bound_range(const KeyType &key, KeyValueCompare comp) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair + lower_bound_range(const_reference value) const + { return this->lower_bound_range(value, this->comp()); } + + template + std::pair + lower_bound_range(const KeyType &key, KeyValueCompare comp) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::lower_bound_range(this->header_ptr(), key, key_node_comp)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //bounded_range + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) + { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } + + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::bounded_range + (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); + return std::pair( iterator(ret.first, this->priv_value_traits_ptr()) + , iterator(ret.second, this->priv_value_traits_ptr())); + } + + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const + { return this->bounded_range(lower_value, upper_value, this->comp(), left_closed, right_closed); } + + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const + { + detail::key_nodeptr_comp + key_node_comp(comp, &this->get_value_traits()); + std::pair ret + (node_algorithms::bounded_range + (this->header_ptr(), lower_key, upper_key, key_node_comp, left_closed, right_closed)); + return std::pair( const_iterator(ret.first, this->priv_value_traits_ptr()) + , const_iterator(ret.second, this->priv_value_traits_ptr())); + } + + //insert_unique_check + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + ocomp(key_value_comp, &this->get_value_traits()); + std::pair ret = + (node_algorithms::insert_unique_check + (this->header_ptr(), key, ocomp, commit_data)); + return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); + } + + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data) + { + detail::key_nodeptr_comp + ocomp(key_value_comp, &this->get_value_traits()); + std::pair ret = + (node_algorithms::insert_unique_check + (this->header_ptr(), hint.pointed_node(), key, ocomp, commit_data)); + return std::pair(iterator(ret.first, this->priv_value_traits_ptr()), ret.second); + } +}; + +//Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member +//in the first position, but if size is not going to be stored then we'll use an specialization +//that doesn't inherit from size_holder +template +struct bstbase_hack + : public detail::size_holder + , public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> +{ + typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; + typedef typename base_type::value_compare value_compare; + typedef SizeType size_type; + typedef typename base_type::node_traits node_traits; + typedef typename get_algo + ::type algo_type; + + bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + { + this->sz_traits().set_size(size_type(0)); + } + + typedef detail::size_holder size_traits; + + size_traits &sz_traits() + { return static_cast(*this); } + + const size_traits &sz_traits() const + { return static_cast(*this); } +}; + +//Specialization for ConstantTimeSize == false +template +struct bstbase_hack + : public bstbase2 < ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> +{ + typedef bstbase2< ValueTraits, VoidOrKeyComp, AlgoType, HeaderHolder> base_type; + typedef typename base_type::value_compare value_compare; + bstbase_hack(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + {} + + typedef detail::size_holder size_traits; + + size_traits &sz_traits() + { return s_size_traits; } + + const size_traits &sz_traits() const + { return s_size_traits; } + + static size_traits s_size_traits; +}; + +template +detail::size_holder bstbase_hack::s_size_traits; + +//This class will +template +struct bstbase + : public bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> +{ + typedef bstbase_hack< ValueTraits, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type; + typedef ValueTraits value_traits; + typedef typename base_type::value_compare value_compare; + typedef value_compare key_compare; + typedef typename base_type::const_reference const_reference; + typedef typename base_type::reference reference; + typedef typename base_type::iterator iterator; + typedef typename base_type::const_iterator const_iterator; + typedef typename base_type::node_traits node_traits; + typedef typename get_algo + ::type node_algorithms; + typedef SizeType size_type; + + bstbase(const value_compare & comp, const ValueTraits &vtraits) + : base_type(comp, vtraits) + {} + + //Detach all inserted nodes. This will add exception safety to bstree_impl + //constructors inserting elements. + ~bstbase() + { + if(is_safe_autounlink::value){ + node_algorithms::clear_and_dispose + ( this->header_ptr() + , detail::node_disposer + (detail::null_disposer(), &this->get_value_traits())); + node_algorithms::init(this->header_ptr()); + } + } +}; + + +/// @endcond + +//! The class template bstree is an unbalanced intrusive binary search tree +//! container. The no-throw guarantee holds only, if the value_compare object +//! doesn't throw. +//! +//! The complexity guarantees only hold if the tree is balanced, logarithmic +//! complexity would increase to linear if the tree is totally unbalanced. +//! +//! The template parameter \c T is the type to be managed by the container. +//! The user can specify additional options and if no options are provided +//! default options are used. +//! +//! The container supports the following options: +//! \c base_hook<>/member_hook<>/value_traits<>, +//! \c constant_time_size<>, \c size_type<> and +//! \c compare<>. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +class bstree_impl + : public bstbase +{ + public: + /// @cond + typedef bstbase data_type; + typedef tree_iterator iterator_type; + typedef tree_iterator const_iterator_type; + /// @endcond + + typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits) value_traits; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::element_type) value_type; + typedef BOOST_INTRUSIVE_IMPDEF(value_type) key_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::reference) const_reference; + typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits::difference_type) difference_type; + typedef BOOST_INTRUSIVE_IMPDEF(SizeType) size_type; + typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare) value_compare; + typedef BOOST_INTRUSIVE_IMPDEF(value_compare) key_compare; + typedef BOOST_INTRUSIVE_IMPDEF(iterator_type) iterator; + typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type) const_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::detail::reverse_iterator) reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::detail::reverse_iterator) const_reverse_iterator; + typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits) node_traits; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node) node; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr) node_ptr; + typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr) const_node_ptr; + /// @cond + typedef typename get_algo::type algo_type; + /// @endcond + typedef BOOST_INTRUSIVE_IMPDEF(algo_type) node_algorithms; + + static const bool constant_time_size = ConstantTimeSize; + static const bool stateful_value_traits = detail::is_stateful_value_traits::value; + /// @cond + private: + + //noncopyable + BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl) + + static const bool safemode_or_autounlink = is_safe_autounlink::value; + + //Constant-time size is incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink))); + + + protected: + + + /// @endcond + + public: + + typedef typename node_algorithms::insert_commit_data insert_commit_data; + + //! Effects: Constructs an empty container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor of the value_compare object throws. Basic guarantee. + explicit bstree_impl( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_type(cmp, v_traits) + {} + + //! Requires: Dereferencing iterator must yield an lvalue of type value_type. + //! cmp must be a comparison function that induces a strict weak ordering. + //! + //! Effects: Constructs an empty container and inserts elements from + //! [b, e). + //! + //! Complexity: Linear in N if [b, e) is already sorted using + //! comp and otherwise N * log N, where N is the distance between first and last. + //! + //! Throws: If value_traits::node_traits::node + //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) + //! or the copy constructor/operator() of the value_compare object throws. Basic guarantee. + template + bstree_impl( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : data_type(cmp, v_traits) + { + //bstbase releases elements in case of exceptions + if(unique) + this->insert_unique(b, e); + else + this->insert_equal(b, e); + } + + //! Effects: to-do + //! + bstree_impl(BOOST_RV_REF(bstree_impl) x) + : data_type(::boost::move(x.comp()), ::boost::move(x.get_value_traits())) + { + this->swap(x); + } + + //! Effects: to-do + //! + bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x) + { this->swap(x); return *this; } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + //! Effects: Detaches all elements from this. The objects in the set + //! are not deleted (i.e. no destructors are called), but the nodes according to + //! the value_traits template parameter are reinitialized and thus can be reused. + //! + //! Complexity: Linear to elements contained in *this. + //! + //! Throws: Nothing. + ~bstree_impl() + {} + + //! Effects: Returns an iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator begin(); + + //! Effects: Returns a const_iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator begin() const; + + //! Effects: Returns a const_iterator pointing to the beginning of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cbegin() const; + + //! Effects: Returns an iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator end(); + + //! Effects: Returns a const_iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator end() const; + + //! Effects: Returns a const_iterator pointing to the end of the container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator cend() const; + + //! Effects: Returns a reverse_iterator pointing to the beginning of the + //! reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rbegin(); + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rbegin() const; + + //! Effects: Returns a const_reverse_iterator pointing to the beginning + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crbegin() const; + + //! Effects: Returns a reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + reverse_iterator rend(); + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator rend() const; + + //! Effects: Returns a const_reverse_iterator pointing to the end + //! of the reversed container. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_reverse_iterator crend() const; + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Precondition: end_iterator must be a valid end iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static bstree_impl &container_from_end_iterator(iterator end_iterator) + { + return static_cast + (data_type::get_tree_base_from_end_iterator(end_iterator)); + } + + //! Precondition: end_iterator must be a valid end const_iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const bstree_impl &container_from_end_iterator(const_iterator end_iterator) + { + return static_cast + (data_type::get_tree_base_from_end_iterator(end_iterator)); + } + + //! Precondition: it must be a valid iterator + //! of the container. + //! + //! Effects: Returns a const reference to the container associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static bstree_impl &container_from_iterator(iterator it) + { return container_from_end_iterator(it.end_iterator_from_it()); } + + //! Precondition: it must be a valid end const_iterator + //! of container. + //! + //! Effects: Returns a const reference to the container associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const bstree_impl &container_from_iterator(const_iterator it) + { return container_from_end_iterator(it.end_iterator_from_it()); } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Returns the key_compare object used by the container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + key_compare key_comp() const; + + //! Effects: Returns the value_compare object used by the container. + //! + //! Complexity: Constant. + //! + //! Throws: If value_compare copy-constructor throws. + value_compare value_comp() const; + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Returns true if the container is empty. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + bool empty() const + { + if(ConstantTimeSize){ + return !this->data_type::sz_traits().get_size(); + } + else{ + return algo_type::unique(this->header_ptr()); + } + } + + //! Effects: Returns the number of elements stored in the container. + //! + //! Complexity: Linear to elements contained in *this + //! if constant-time size option is disabled. Constant time otherwise. + //! + //! Throws: Nothing. + size_type size() const + { + if(constant_time_size) + return this->sz_traits().get_size(); + else{ + return (size_type)node_algorithms::size(this->header_ptr()); + } + } + + //! Effects: Swaps the contents of two containers. + //! + //! Complexity: Constant. + //! + //! Throws: If the comparison functor's swap call throws. + void swap(bstree_impl& other) + { + //This can throw + using std::swap; + swap(this->comp(), this->comp()); + //These can't throw + node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr())); + if(constant_time_size){ + size_type backup = this->sz_traits().get_size(); + this->sz_traits().set_size(other.sz_traits().get_size()); + other.sz_traits().set_size(backup); + } + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! Cloner should yield to nodes equivalent to the original nodes. + //! + //! Effects: Erases all the elements from *this + //! calling Disposer::operator()(pointer), clones all the + //! elements from src calling Cloner::operator()(const_reference ) + //! and inserts them on *this. Copies the predicate from the source container. + //! + //! If cloner throws, all cloned elements are unlinked and disposed + //! calling Disposer::operator()(pointer). + //! + //! Complexity: Linear to erased plus inserted elements. + //! + //! Throws: If cloner throws or predicate copy assignment throws. Basic guarantee. + template + void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer) + { + this->clear_and_dispose(disposer); + if(!src.empty()){ + detail::exception_disposer + rollback(*this, disposer); + node_algorithms::clone + (const_node_ptr(src.header_ptr()) + ,node_ptr(this->header_ptr()) + ,detail::node_cloner (cloner, &this->get_value_traits()) + ,detail::node_disposer(disposer, &this->get_value_traits())); + this->sz_traits().set_size(src.sz_traits().get_size()); + this->comp() = src.comp(); + rollback.release(); + } + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the container before the upper bound. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(reference value) + { + detail::key_nodeptr_comp + key_node_comp(this->comp(), &this->get_value_traits()); + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + iterator ret(node_algorithms::insert_equal_upper_bound + (this->header_ptr(), to_insert, key_node_comp), this->priv_value_traits_ptr()); + this->sz_traits().increment(); + return ret; + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator. + //! + //! Effects: Inserts x into the container, using "hint" as a hint to + //! where it will be inserted. If "hint" is the upper_bound + //! the insertion takes constant time (two comparisons in the worst case) + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the internal value_compare ordering function throws. Strong guarantee. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_equal(const_iterator hint, reference value) + { + detail::key_nodeptr_comp + key_node_comp(this->comp(), &this->get_value_traits()); + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + iterator ret(node_algorithms::insert_equal + (this->header_ptr(), hint.pointed_node(), to_insert, key_node_comp), this->priv_value_traits_ptr()); + this->sz_traits().increment(); + return ret; + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Inserts a each element of a range into the container + //! before the upper bound of the key of each element. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_equal(Iterator b, Iterator e) + { + iterator iend(this->end()); + for (; b != e; ++b) + this->insert_equal(iend, *b); + } + + //! Requires: value must be an lvalue + //! + //! Effects: Inserts value into the container if the value + //! is not already present. + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + std::pair insert_unique(reference value) + { + insert_commit_data commit_data; + std::pair ret = this->insert_unique_check(value, this->comp(), commit_data); + if(!ret.second) + return ret; + return std::pair (this->insert_unique_commit(value, commit_data), true); + } + + //! Requires: value must be an lvalue, and "hint" must be + //! a valid iterator + //! + //! Effects: Tries to insert x into the container, using "hint" as a hint + //! to where it will be inserted. + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time (two comparisons in the worst case) + //! if t is inserted immediately before hint. + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + iterator insert_unique(const_iterator hint, reference value) + { + insert_commit_data commit_data; + std::pair ret = this->insert_unique_check(hint, value, this->comp(), commit_data); + if(!ret.second) + return ret.first; + return this->insert_unique_commit(value, commit_data); + } + + //! Requires: Dereferencing iterator must yield an lvalue + //! of type value_type. + //! + //! Effects: Tries to insert each element of a range into the container. + //! + //! Complexity: Insert range is in general O(N * log(N)), where N is the + //! size of the range. However, it is linear in N if the range is already sorted + //! by value_comp(). + //! + //! Throws: Nothing. + //! + //! Note: Does not affect the validity of iterators and references. + //! No copy-constructors are called. + template + void insert_unique(Iterator b, Iterator e) + { + if(this->empty()){ + iterator iend(this->end()); + for (; b != e; ++b) + this->insert_unique(iend, *b); + } + else{ + for (; b != e; ++b) + this->insert_unique(*b); + } + } + + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the container, using + //! a user provided key instead of the value itself. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the value_type and this function offers the possibility to use that + //! part to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the value_type and use + //! "insert_commit" to insert the object in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_commit" only if no more + //! objects are inserted or erased from the container. + template + std::pair insert_unique_check + (const KeyType &key, KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + //! Requires: key_value_comp must be a comparison function that induces + //! the same strict weak ordering as value_compare. The difference is that + //! key_value_comp compares an arbitrary key with the contained values. + //! + //! Effects: Checks if a value can be inserted in the container, using + //! a user provided key instead of the value itself, using "hint" + //! as a hint to where it will be inserted. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing an iterator to the already present value + //! and false. If the value can be inserted returns true in the returned + //! pair boolean and fills "commit_data" that is meant to be used with + //! the "insert_commit" function. + //! + //! Complexity: Logarithmic in general, but it's amortized + //! constant time if t is inserted immediately before hint. + //! + //! Throws: If the key_value_comp ordering function throws. Strong guarantee. + //! + //! Notes: This function is used to improve performance when constructing + //! a value_type is expensive: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! constructing that is used to impose the order is much cheaper to construct + //! than the value_type and this function offers the possibility to use that key + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the value_type and use + //! "insert_commit" to insert the object in constant-time. This can give a total + //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_commit" only if no more + //! objects are inserted or erased from the container. + template + std::pair insert_unique_check + (const_iterator hint, const KeyType &key + ,KeyValueCompare key_value_comp, insert_commit_data &commit_data); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Requires: value must be an lvalue of type value_type. commit_data + //! must have been obtained from a previous call to "insert_check". + //! No objects should have been inserted or erased from the container between + //! the "insert_check" that filled "commit_data" and the call to "insert_commit". + //! + //! Effects: Inserts the value in the container using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Returns: An iterator to the newly inserted object. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + node_algorithms::insert_unique_commit + (this->header_ptr(), to_insert, commit_data); + this->sz_traits().increment(); + return iterator(to_insert, this->priv_value_traits_ptr()); + } + + //! Requires: value must be an lvalue, "pos" must be + //! a valid iterator (or end) and must be the succesor of value + //! once inserted according to the predicate + //! + //! Effects: Inserts x into the container before "pos". + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if "pos" is not + //! the successor of "value" container ordering invariant will be broken. + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + iterator insert_before(const_iterator pos, reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + return iterator(node_algorithms::insert_before + (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr()); + } + + //! Requires: value must be an lvalue, and it must be no less + //! than the greatest inserted key + //! + //! Effects: Inserts x into the container in the last position. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if value is + //! less than the greatest inserted key container ordering invariant will be broken. + //! This function is slightly more efficient than using "insert_before". + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + void push_back(reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + node_algorithms::push_back(this->header_ptr(), to_insert); + } + + //! Requires: value must be an lvalue, and it must be no greater + //! than the minimum inserted key + //! + //! Effects: Inserts x into the container in the first position. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Note: This function does not check preconditions so if value is + //! greater than the minimum inserted key container ordering invariant will be broken. + //! This function is slightly more efficient than using "insert_before". + //! This is a low-level function to be used only for performance reasons + //! by advanced users. + void push_front(reference value) + { + node_ptr to_insert(this->get_value_traits().to_node_ptr(value)); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + this->sz_traits().increment(); + node_algorithms::push_front(this->header_ptr(), to_insert); + } + + //! Effects: Erases the element pointed to by pos. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(const_iterator i) + { + const_iterator ret(i); + ++ret; + node_ptr to_erase(i.pointed_node()); + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase)); + node_algorithms::erase(this->header_ptr(), to_erase); + this->sz_traits().decrement(); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + return ret.unconst(); + } + + //! Effects: Erases the range pointed to by b end e. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + iterator erase(const_iterator b, const_iterator e) + { size_type n; return this->private_erase(b, e, n); } + + //! Effects: Erases all the elements with the given value. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + size_type erase(const_reference value) + { return this->erase(value, this->comp()); } + + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase(const KeyType& key, KeyValueCompare comp + /// @cond + , typename detail::enable_if_c::value >::type * = 0 + /// @endcond + ) + { + std::pair p = this->equal_range(key, comp); + size_type n; + this->private_erase(p.first, p.second, n); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the element pointed to by pos. + //! Disposer::operator()(pointer) is called for the removed element. + //! + //! Complexity: Average complexity for erase element is constant time. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(const_iterator i, Disposer disposer) + { + node_ptr to_erase(i.pointed_node()); + iterator ret(this->erase(i)); + disposer(this->get_value_traits().to_value_ptr(to_erase)); + return ret; + } + + #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + template + iterator erase_and_dispose(iterator i, Disposer disposer) + { return this->erase_and_dispose(const_iterator(i), disposer); } + #endif + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given value. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + template + size_type erase_and_dispose(const_reference value, Disposer disposer) + { + std::pair p = this->equal_range(value); + size_type n; + this->private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases the range pointed to by b end e. + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Complexity: Average complexity for erase range is at most + //! O(log(size() + N)), where N is the number of elements in the range. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) + { size_type n; return this->private_erase(b, e, n, disposer); } + + //! Requires: Disposer::operator()(pointer) shouldn't throw. + //! + //! Effects: Erases all the elements with the given key. + //! according to the comparison functor "comp". + //! Disposer::operator()(pointer) is called for the removed elements. + //! + //! Returns: The number of erased elements. + //! + //! Complexity: O(log(size() + N). + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators + //! to the erased elements. + template + size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer + /// @cond + , typename detail::enable_if_c::value >::type * = 0 + /// @endcond + ) + { + std::pair p = this->equal_range(key, comp); + size_type n; + this->private_erase(p.first, p.second, n, disposer); + return n; + } + + //! Effects: Erases all of the elements. + //! + //! Complexity: Linear to the number of elements on the container. + //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. No destructors are called. + void clear() + { + if(safemode_or_autounlink){ + this->clear_and_dispose(detail::null_disposer()); + } + else{ + node_algorithms::init_header(this->header_ptr()); + this->sz_traits().set_size(0); + } + } + + //! Effects: Erases all of the elements calling disposer(p) for + //! each node to be erased. + //! Complexity: Average complexity for is at most O(log(size() + N)), + //! where N is the number of elements in the container. + //! + //! Throws: Nothing. + //! + //! Note: Invalidates the iterators (but not the references) + //! to the erased elements. Calls N times to disposer functor. + template + void clear_and_dispose(Disposer disposer) + { + node_algorithms::clear_and_dispose(this->header_ptr() + , detail::node_disposer(disposer, &this->get_value_traits())); + node_algorithms::init_header(this->header_ptr()); + this->sz_traits().set_size(0); + } + + //! Effects: Returns the number of contained elements with the given value + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given value. + //! + //! Throws: If `value_compare` throws. + size_type count(const_reference value) const + { return size_type(this->count(value, this->comp())); } + + //! Effects: Returns the number of contained elements with the given key + //! + //! Complexity: Logarithmic to the number of elements contained plus lineal + //! to number of objects with the given key. + //! + //! Throws: If `comp` throws. + template + size_type count(const KeyType &key, KeyValueCompare comp) const + { + std::pair ret = this->equal_range(key, comp); + size_type n = 0; + for(; ret.first != ret.second; ++ret.first){ ++n; } + return n; + } + + #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //Add non-const overloads to theoretically const members + //as some algorithms have different behavior when non-const versions are used (like splay trees). + size_type count(const_reference value) + { return size_type(this->count(value, this->comp())); } + + template + size_type count(const KeyType &key, KeyValueCompare comp) + { + std::pair ret = this->equal_range(key, comp); + size_type n = 0; + for(; ret.first != ret.second; ++ret.first){ ++n; } + return n; + } + + #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator lower_bound(const_reference value); + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator lower_bound(const_reference value) const; + + //! Effects: Returns an iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator lower_bound(const KeyType &key, KeyValueCompare comp); + + //! Effects: Returns a const iterator to the first element whose + //! key is not less than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator upper_bound(const_reference value); + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator upper_bound(const KeyType &key, KeyValueCompare comp); + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator upper_bound(const_reference value) const; + + //! Effects: Returns an iterator to the first element whose + //! key is greater than k according to comp or end() if that element + //! does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + iterator find(const_reference value); + + //! Effects: Finds an iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + iterator find(const KeyType &key, KeyValueCompare comp); + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + const_iterator find(const_reference value) const; + + //! Effects: Finds a const_iterator to the first element whose key is + //! k or end() if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + const_iterator find(const KeyType &key, KeyValueCompare comp) const; + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + std::pair equal_range(const_reference value); + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + std::pair equal_range(const KeyType &key, KeyValueCompare comp); + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + std::pair + equal_range(const_reference value) const; + + //! Effects: Finds a range containing all elements whose key is k or + //! an empty range that indicates the position where those elements would be + //! if they there is no elements with key k. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + template + std::pair + equal_range(const KeyType &key, KeyValueCompare comp) const; + + //! Requires: 'lower_value' must not be greater than 'upper_value'. If + //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_value and upper_value. + //! + //! Note: Experimental function, the interface might change in future releases. + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed); + + //! Requires: KeyValueCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the container. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise + //! + //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change in future releases. + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed); + + //! Requires: 'lower_value' must not be greater than 'upper_value'. If + //! 'lower_value' == 'upper_value', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `value_compare` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_value and upper_value. + //! + //! Note: Experimental function, the interface might change in future releases. + std::pair bounded_range + (const_reference lower_value, const_reference upper_value, bool left_closed, bool right_closed) const; + + //! Requires: KeyValueCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the container. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise + //! + //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If `comp` throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change in future releases. + template + std::pair bounded_range + (const KeyType &lower_key, const KeyType &upper_key, KeyValueCompare comp, bool left_closed, bool right_closed) const; + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static iterator s_iterator_to(reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This static function is available only if the value traits + //! is stateless. + static const_iterator s_iterator_to(const_reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid iterator i belonging to the set + //! that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + iterator iterator_to(reference value); + + //! Requires: value must be an lvalue and shall be in a set of + //! appropriate type. Otherwise the behavior is undefined. + //! + //! Effects: Returns: a valid const_iterator i belonging to the + //! set that points to the value + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + const_iterator iterator_to(const_reference value) const; + + //! Requires: value shall not be in a container. + //! + //! Effects: init_node puts the hook of a value in a well-known default + //! state. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant time. + //! + //! Note: This function puts the hook in the well-known default state + //! used by auto_unlink and safe hooks. + static void init_node(reference value); + + #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Unlinks the leftmost node from the container. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the container and the container can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the container. + pointer unlink_leftmost_without_rebalance() + { + node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance + (this->header_ptr())); + if(!to_be_disposed) + return 0; + this->sz_traits().decrement(); + if(safemode_or_autounlink)//If this is commented does not work with normal_link + node_algorithms::init(to_be_disposed); + return this->get_value_traits().to_value_ptr(to_be_disposed); + } + + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Requires: replace_this must be a valid iterator of *this + //! and with_this must not be inserted in any container. + //! + //! Effects: Replaces replace_this in its position in the + //! container with with_this. The container does not need to be rebalanced. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! with_this is not equivalent to *replace_this according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. + void replace_node(iterator replace_this, reference with_this); + + //! Effects: Rebalances the tree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + void rebalance(); + + //! Requires: old_root is a node of a tree. + //! + //! Effects: Rebalances the subtree rooted at old_root. + //! + //! Returns: The new root of the subtree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the elements in the subtree. + iterator rebalance_subtree(iterator root); + + #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: removes "value" from the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic time. + //! + //! Note: This static function is only usable with non-constant + //! time size containers that have stateless comparison functors. + //! + //! If the user calls + //! this function with a constant time size container or stateful comparison + //! functor a compilation error will be issued. + static void remove_node(reference value) + { + BOOST_STATIC_ASSERT((!constant_time_size)); + node_ptr to_remove(value_traits::to_node_ptr(value)); + node_algorithms::unlink(to_remove); + if(safemode_or_autounlink) + node_algorithms::init(to_remove); + } + + //! Effects: Asserts the integrity of the container with additional checks provided by the user. + //! + //! Complexity: Linear time. + //! + //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + template + void check(ExtraChecker extra_checker) const + { + typedef detail::key_nodeptr_comp nodeptr_comp_t; + nodeptr_comp_t nodeptr_comp(this->comp(), &this->get_value_traits()); + typedef typename get_node_checker::type node_checker_t; + typename node_checker_t::return_type checker_return; + node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return); + if (constant_time_size) + BOOST_INTRUSIVE_INVARIANT_ASSERT(this->sz_traits().get_size() == checker_return.node_count); + } + + //! Effects: Asserts the integrity of the container. + //! + //! Complexity: Linear time. + //! + //! Note: The method has no effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + void check() const + { + check(detail::empty_node_checker()); + } + + /// @cond + private: + template + iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer) + { + for(n = 0; b != e; ++n) + this->erase_and_dispose(b++, disposer); + return b.unconst(); + } + + iterator private_erase(const_iterator b, const_iterator e, size_type &n) + { + for(n = 0; b != e; ++n) + this->erase(b++); + return b.unconst(); + } + /// @endcond +}; + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator< +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +bool operator== +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ + typedef bstree_impl tree_type; + typedef typename tree_type::const_iterator const_iterator; + + if(tree_type::constant_time_size && x.size() != y.size()){ + return false; + } + const_iterator end1 = x.end(); + const_iterator i1 = x.begin(); + const_iterator i2 = y.begin(); + if(tree_type::constant_time_size){ + while (i1 != end1 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1; + } + else{ + const_iterator end2 = y.end(); + while (i1 != end1 && i2 != end2 && *i1 == *i2) { + ++i1; + ++i2; + } + return i1 == end1 && i2 == end2; + } +} + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator!= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(x == y); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator> +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return y < x; } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator<= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(y < x); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline bool operator>= +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(const bstree_impl &x, const bstree_impl &y) +#else +( const bstree_impl &x +, const bstree_impl &y) +#endif +{ return !(x < y); } + +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +template +#else +template +#endif +inline void swap +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) +(bstree_impl &x, bstree_impl &y) +#else +( bstree_impl &x +, bstree_impl &y) +#endif +{ x.swap(y); } + +//! Helper metafunction to define a \c bstree that yields to the same type when the +//! same options (either explicitly or implicitly) are used. +#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +struct make_bstree +{ + /// @cond + typedef typename pack_options + < bstree_defaults, + #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) + O1, O2, O3, O4, O5 + #else + Options... + #endif + >::type packed_options; + + typedef typename detail::get_value_traits + ::type value_traits; + typedef typename detail::get_header_holder_type + < value_traits, typename packed_options::header_holder_type >::type header_holder_type; + + typedef bstree_impl + < value_traits + , typename packed_options::compare + , typename packed_options::size_type + , packed_options::constant_time_size + , BsTreeAlgorithms + , header_holder_type + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +#if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) +template +#else +template +#endif +class bstree + : public make_bstree::type +{ + typedef typename make_bstree + ::type Base; + BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree) + + public: + typedef typename Base::value_compare value_compare; + typedef typename Base::value_traits value_traits; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + + //Assert if passed value traits are compatible with the type + BOOST_STATIC_ASSERT((detail::is_same::value)); + + bstree( const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(cmp, v_traits) + {} + + template + bstree( bool unique, Iterator b, Iterator e + , const value_compare &cmp = value_compare() + , const value_traits &v_traits = value_traits()) + : Base(unique, b, e, cmp, v_traits) + {} + + bstree(BOOST_RV_REF(bstree) x) + : Base(::boost::move(static_cast(x))) + {} + + bstree& operator=(BOOST_RV_REF(bstree) x) + { return static_cast(this->Base::operator=(::boost::move(static_cast(x)))); } + + static bstree &container_from_end_iterator(iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static const bstree &container_from_end_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static bstree &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const bstree &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } +}; + +#endif +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BSTREE_HPP diff --git a/boost/intrusive/bstree_algorithms.hpp b/boost/intrusive/bstree_algorithms.hpp new file mode 100644 index 0000000..de5445e --- /dev/null +++ b/boost/intrusive/bstree_algorithms.hpp @@ -0,0 +1,2127 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond + +//! This type is the information that will be filled by insert_unique_check +template +struct insert_commit_data_t +{ + insert_commit_data_t() + : link_left(false) + , node() + {} + bool link_left; + NodePtr node; +}; + +template +struct data_for_rebalance_t +{ + NodePtr x; + NodePtr x_parent; + NodePtr y; +}; + +namespace detail { + +template +struct bstree_node_checker + : public ExtraChecker +{ + typedef ExtraChecker base_checker_t; + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type + : public base_checker_t::return_type + { + return_type() : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0) {} + + const_node_ptr min_key_node_ptr; + const_node_ptr max_key_node_ptr; + size_t node_count; + }; + + bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker) + : base_checker_t(extra_checker), comp_(comp) + {} + + void operator () (const const_node_ptr& p, + const return_type& check_return_left, const return_type& check_return_right, + return_type& check_return) + { + if (check_return_left.max_key_node_ptr) + BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr)); + if (check_return_right.min_key_node_ptr) + BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p)); + check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p; + check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p; + check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1; + base_checker_t::operator()(p, check_return_left, check_return_right, check_return); + } + + const NodePtrCompare comp_; +}; + +} // namespace detail + +/// @endcond + + + +//! This is an implementation of a binary search tree. +//! A node in the search tree has references to its children and its parent. This +//! is to allow traversal of the whole tree from a given node making the +//! implementation of iterator a pointer to a node. +//! At the top of the tree a node is used specially. This node's parent pointer +//! is pointing to the root of the tree. Its left pointer points to the +//! leftmost node in the tree and the right pointer to the rightmost one. +//! This node is used to represent the end-iterator. +//! +//! +---------+ +//! header------------------------------>| | +//! | | +//! +----------(left)--------| |--------(right)---------+ +//! | +---------+ | +//! | | | +//! | | (parent) | +//! | | | +//! | | | +//! | +---------+ | +//! root of tree ..|......................> | | | +//! | | D | | +//! | | | | +//! | +-------+---------+-------+ | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | | | | +//! | +---------+ +---------+ | +//! | | | | | | +//! | | B | | F | | +//! | | | | | | +//! | +--+---------+--+ +--+---------+--+ | +//! | | | | | | +//! | | | | | | +//! | | | | | | +//! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ | +//! +-->| | | | | | | |<--+ +//! | A | | C | | E | | G | +//! | | | | | | | | +//! +---------+ +---------+ +---------+ +---------+ +//! +//! bstree_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the binary search tree +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_parent(const_node_ptr n); +//! +//! static void set_parent(node_ptr n, node_ptr parent); +//! +//! static node_ptr get_left(const_node_ptr n); +//! +//! static void set_left(node_ptr n, node_ptr left); +//! +//! static node_ptr get_right(const_node_ptr n); +//! +//! static void set_right(node_ptr n, node_ptr right); +template +class bstree_algorithms +{ + public: + typedef typename NodeTraits::node node; + typedef NodeTraits node_traits; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef insert_commit_data_t insert_commit_data; + typedef data_for_rebalance_t data_for_rebalance; + + /// @cond + + private: + template + struct dispose_subtree_disposer + { + dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree) + : disposer_(&disp), subtree_(subtree) + {} + + void release() + { disposer_ = 0; } + + ~dispose_subtree_disposer() + { + if(disposer_){ + dispose_subtree(subtree_, *disposer_); + } + } + Disposer *disposer_; + const node_ptr subtree_; + }; + + /// @endcond + + public: + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the first node of the tree, the header if the tree is empty. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr begin_node(const const_node_ptr & header) + { return node_traits::get_left(header); } + + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the header of the tree. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr end_node(const const_node_ptr & header) + { return detail::uncast(header); } + + //! Requires: 'header' is the header node of a tree. + //! + //! Effects: Returns the root of the tree if any, header otherwise + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static node_ptr root_node(const const_node_ptr & header) + { + node_ptr p = node_traits::get_parent(header); + return p ? p : detail::uncast(header); + } + + //! Requires: 'node' is a node of the tree or a node initialized + //! by init(...) or init_node. + //! + //! Effects: Returns true if the node is initialized by init() or init_node(). + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + static bool unique(const const_node_ptr & node) + { return !NodeTraits::get_parent(node); } + + //! Requires: 'node' is a node of the tree or a header node. + //! + //! Effects: Returns the header of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(const const_node_ptr & node) + { + node_ptr n(detail::uncast(node)); + node_ptr p(NodeTraits::get_parent(node)); + //If p is null, then n is the header of an empty tree + if(p){ + //Non-empty tree, check if n is neither root nor header + node_ptr pp(NodeTraits::get_parent(p)); + //If granparent is not equal to n, then n is neither root nor header, + //the try the fast path + if(n != pp){ + do{ + n = p; + p = pp; + pp = NodeTraits::get_parent(pp); + }while(n != pp); + n = p; + } + //Check if n is root or header when size() > 0 + else if(!is_header(n)){ + n = p; + } + } + return n; + /* + node_ptr h = detail::uncast(node); + node_ptr p = NodeTraits::get_parent(node); + if(p){ + while(!is_header(p)) + p = NodeTraits::get_parent(p); + return p; + } + else{ + return h; + }*/ + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(const node_ptr & node1, const node_ptr & node2) + { + if(node1 == node2) + return; + + node_ptr header1(get_header(node1)), header2(get_header(node2)); + swap_nodes(node1, header1, node2, header2); + } + + //! Requires: node1 and node2 can't be header nodes + //! of two trees with header header1 and header2. + //! + //! Effects: Swaps two nodes. After the function node1 will be inserted + //! in the position node2 before the function. node2 will be inserted in the + //! position node1 had before the function. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! node1 and node2 are not equivalent according to the ordering rules. + //! + //!Experimental function + static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2) + { + if(node1 == node2) + return; + + //node1 and node2 must not be header nodes + //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2)); + if(header1 != header2){ + //Update header1 if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + + //Update header2 if necessary + if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + } + else{ + //If both nodes are from the same tree + //Update header if necessary + if(node1 == NodeTraits::get_left(header1)){ + NodeTraits::set_left(header1, node2); + } + else if(node2 == NodeTraits::get_left(header2)){ + NodeTraits::set_left(header2, node1); + } + + if(node1 == NodeTraits::get_right(header1)){ + NodeTraits::set_right(header1, node2); + } + else if(node2 == NodeTraits::get_right(header2)){ + NodeTraits::set_right(header2, node1); + } + + if(node1 == NodeTraits::get_parent(header1)){ + NodeTraits::set_parent(header1, node2); + } + else if(node2 == NodeTraits::get_parent(header2)){ + NodeTraits::set_parent(header2, node1); + } + + //Adjust data in nodes to be swapped + //so that final link swap works as expected + if(node1 == NodeTraits::get_parent(node2)){ + NodeTraits::set_parent(node2, node2); + + if(node2 == NodeTraits::get_right(node1)){ + NodeTraits::set_right(node1, node1); + } + else{ + NodeTraits::set_left(node1, node1); + } + } + else if(node2 == NodeTraits::get_parent(node1)){ + NodeTraits::set_parent(node1, node1); + + if(node1 == NodeTraits::get_right(node2)){ + NodeTraits::set_right(node2, node2); + } + else{ + NodeTraits::set_left(node2, node2); + } + } + } + + //Now swap all the links + node_ptr temp; + //swap left link + temp = NodeTraits::get_left(node1); + NodeTraits::set_left(node1, NodeTraits::get_left(node2)); + NodeTraits::set_left(node2, temp); + //swap right link + temp = NodeTraits::get_right(node1); + NodeTraits::set_right(node1, NodeTraits::get_right(node2)); + NodeTraits::set_right(node2, temp); + //swap parent link + temp = NodeTraits::get_parent(node1); + NodeTraits::set_parent(node1, NodeTraits::get_parent(node2)); + NodeTraits::set_parent(node2, temp); + + //Now adjust adjacent nodes for newly inserted node 1 + if((temp = NodeTraits::get_left(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_right(node1))){ + NodeTraits::set_parent(temp, node1); + } + if((temp = NodeTraits::get_parent(node1)) && + //The header has been already updated so avoid it + temp != header2){ + if(NodeTraits::get_left(temp) == node2){ + NodeTraits::set_left(temp, node1); + } + if(NodeTraits::get_right(temp) == node2){ + NodeTraits::set_right(temp, node1); + } + } + //Now adjust adjacent nodes for newly inserted node 2 + if((temp = NodeTraits::get_left(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_right(node2))){ + NodeTraits::set_parent(temp, node2); + } + if((temp = NodeTraits::get_parent(node2)) && + //The header has been already updated so avoid it + temp != header1){ + if(NodeTraits::get_left(temp) == node1){ + NodeTraits::set_left(temp, node2); + } + if(NodeTraits::get_right(temp) == node1){ + NodeTraits::set_right(temp, node2); + } + } + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing and comparison is needed. Experimental function + static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node) + { + if(node_to_be_replaced == new_node) + return; + replace_node(node_to_be_replaced, get_header(node_to_be_replaced), new_node); + } + + //! Requires: node_to_be_replaced must be inserted in a tree + //! with header "header" and new_node must not be inserted in a tree. + //! + //! Effects: Replaces node_to_be_replaced in its position in the + //! tree with new_node. The tree does not need to be rebalanced + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Note: This function will break container ordering invariants if + //! new_node is not equivalent to node_to_be_replaced according to the + //! ordering rules. This function is faster than erasing and inserting + //! the node, since no rebalancing or comparison is needed. Experimental function + static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node) + { + if(node_to_be_replaced == new_node) + return; + + //Update header if necessary + if(node_to_be_replaced == NodeTraits::get_left(header)){ + NodeTraits::set_left(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_right(header)){ + NodeTraits::set_right(header, new_node); + } + + if(node_to_be_replaced == NodeTraits::get_parent(header)){ + NodeTraits::set_parent(header, new_node); + } + + //Now set data from the original node + node_ptr temp; + NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced)); + NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced)); + NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced)); + + //Now adjust adjacent nodes for newly inserted node + if((temp = NodeTraits::get_left(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_right(new_node))){ + NodeTraits::set_parent(temp, new_node); + } + if((temp = NodeTraits::get_parent(new_node)) && + //The header has been already updated so avoid it + temp != header){ + if(NodeTraits::get_left(temp) == node_to_be_replaced){ + NodeTraits::set_left(temp, new_node); + } + if(NodeTraits::get_right(temp) == node_to_be_replaced){ + NodeTraits::set_right(temp, new_node); + } + } + } + + //! Requires: 'node' is a node from the tree except the header. + //! + //! Effects: Returns the next node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr next_node(const node_ptr & node) + { + node_ptr const n_right(NodeTraits::get_right(node)); + if(n_right){ + return minimum(n_right); + } + else { + node_ptr n(node); + node_ptr p(NodeTraits::get_parent(n)); + while(n == NodeTraits::get_right(p)){ + n = p; + p = NodeTraits::get_parent(p); + } + return NodeTraits::get_right(n) != p ? p : n; + } + } + + //! Requires: 'node' is a node from the tree except the leftmost node. + //! + //! Effects: Returns the previous node of the tree. + //! + //! Complexity: Average constant time. + //! + //! Throws: Nothing. + static node_ptr prev_node(const node_ptr & node) + { + if(is_header(node)){ + return NodeTraits::get_right(node); + //return maximum(NodeTraits::get_parent(node)); + } + else if(NodeTraits::get_left(node)){ + return maximum(NodeTraits::get_left(node)); + } + else { + node_ptr p(node); + node_ptr x = NodeTraits::get_parent(p); + while(p == NodeTraits::get_left(x)){ + p = x; + x = NodeTraits::get_parent(x); + } + return x; + } + } + + //! Requires: 'node' is a node of a tree but not the header. + //! + //! Effects: Returns the minimum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr minimum(node_ptr node) + { + for(node_ptr p_left = NodeTraits::get_left(node) + ;p_left + ;p_left = NodeTraits::get_left(node)){ + node = p_left; + } + return node; + } + + //! Requires: 'node' is a node of a tree but not the header. + //! + //! Effects: Returns the maximum node of the subtree starting at p. + //! + //! Complexity: Logarithmic to the size of the subtree. + //! + //! Throws: Nothing. + static node_ptr maximum(node_ptr node) + { + for(node_ptr p_right = NodeTraits::get_right(node) + ;p_right + ;p_right = NodeTraits::get_right(node)){ + node = p_right; + } + return node; + } + + //! Requires: 'node' must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(const node_ptr & node) + { + NodeTraits::set_parent(node, node_ptr()); + NodeTraits::set_left(node, node_ptr()); + NodeTraits::set_right(node, node_ptr()); + }; + + //! Effects: Returns true if node is in the same state as if called init(node) + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool inited(const const_node_ptr & node) + { + return !NodeTraits::get_parent(node) && + !NodeTraits::get_left(node) && + !NodeTraits::get_right(node) ; + }; + + //! Requires: node must not be part of any tree. + //! + //! Effects: Initializes the header to represent an empty tree. + //! unique(header) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init_header(const node_ptr & header) + { + NodeTraits::set_parent(header, node_ptr()); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + + //! Requires: "disposer" must be an object function + //! taking a node_ptr parameter and shouldn't throw. + //! + //! Effects: Empties the target tree calling + //! void disposer::operator()(const node_ptr &) for every node of the tree + //! except the header. + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clear_and_dispose(const node_ptr & header, Disposer disposer) + { + node_ptr source_root = NodeTraits::get_parent(header); + if(!source_root) + return; + dispose_subtree(source_root, disposer); + init_header(header); + } + + //! Requires: header is the header of a tree. + //! + //! Effects: Unlinks the leftmost node from the tree, and + //! updates the header link to the new leftmost node. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function breaks the tree and the tree can + //! only be used for more unlink_leftmost_without_rebalance calls. + //! This function is normally used to achieve a step by step + //! controlled destruction of the tree. + static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header) + { + node_ptr leftmost = NodeTraits::get_left(header); + if (leftmost == header) + return node_ptr(); + node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); + node_ptr leftmost_right (NodeTraits::get_right(leftmost)); + bool is_root = leftmost_parent == header; + + if (leftmost_right){ + NodeTraits::set_parent(leftmost_right, leftmost_parent); + NodeTraits::set_left(header, bstree_algorithms::minimum(leftmost_right)); + + if (is_root) + NodeTraits::set_parent(header, leftmost_right); + else + NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); + } + else if (is_root){ + NodeTraits::set_parent(header, node_ptr()); + NodeTraits::set_left(header, header); + NodeTraits::set_right(header, header); + } + else{ + NodeTraits::set_left(leftmost_parent, node_ptr()); + NodeTraits::set_left(header, leftmost_parent); + } + return leftmost; + } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t size(const const_node_ptr & header) + { + node_ptr beg(begin_node(header)); + node_ptr end(end_node(header)); + std::size_t i = 0; + for(;beg != end; beg = next_node(beg)) ++i; + return i; + } + + //! Requires: header1 and header2 must be the header nodes + //! of two trees. + //! + //! Effects: Swaps two trees. After the function header1 will contain + //! links to the second tree and header2 will have links to the first tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static void swap_tree(const node_ptr & header1, const node_ptr & header2) + { + if(header1 == header2) + return; + + node_ptr tmp; + + //Parent swap + tmp = NodeTraits::get_parent(header1); + NodeTraits::set_parent(header1, NodeTraits::get_parent(header2)); + NodeTraits::set_parent(header2, tmp); + //Left swap + tmp = NodeTraits::get_left(header1); + NodeTraits::set_left(header1, NodeTraits::get_left(header2)); + NodeTraits::set_left(header2, tmp); + //Right swap + tmp = NodeTraits::get_right(header1); + NodeTraits::set_right(header1, NodeTraits::get_right(header2)); + NodeTraits::set_right(header2, tmp); + + //Now test parent + node_ptr h1_parent(NodeTraits::get_parent(header1)); + if(h1_parent){ + NodeTraits::set_parent(h1_parent, header1); + } + else{ + NodeTraits::set_left(header1, header1); + NodeTraits::set_right(header1, header1); + } + + node_ptr h2_parent(NodeTraits::get_parent(header2)); + if(h2_parent){ + NodeTraits::set_parent(h2_parent, header2); + } + else{ + NodeTraits::set_left(header2, header2); + NodeTraits::set_right(header2, header2); + } + } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is the header of the tree. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_header(const const_node_ptr & p) + { + node_ptr p_left (NodeTraits::get_left(p)); + node_ptr p_right(NodeTraits::get_right(p)); + if(!NodeTraits::get_parent(p) || //Header condition when empty tree + (p_left && p_right && //Header always has leftmost and rightmost + (p_left == p_right || //Header condition when only node + (NodeTraits::get_parent(p_left) != p || + NodeTraits::get_parent(p_right) != p )) + //When tree size > 1 headers can't be leftmost's + //and rightmost's parent + )){ + return true; + } + return false; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns a node_ptr to the first element that is equivalent to + //! "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr find + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr end = detail::uncast(header); + node_ptr y = lower_bound(header, key, comp); + return (y == end || comp(key, y)) ? end : y; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If + //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be false. + //! + //! Effects: Returns an a pair with the following criteria: + //! + //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise + //! + //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Note: This function can be more efficient than calling upper_bound + //! and lower_bound for lower_key and upper_key. + //! + //! Note: Experimental function, the interface might change. + template< class KeyType, class KeyNodePtrCompare> + static std::pair bounded_range + ( const const_node_ptr & header + , const KeyType &lower_key + , const KeyType &upper_key + , KeyNodePtrCompare comp + , bool left_closed + , bool right_closed) + { + node_ptr y = detail::uncast(header); + node_ptr x = NodeTraits::get_parent(header); + + while(x){ + //If x is less than lower_key the target + //range is on the right part + if(comp(x, lower_key)){ + //Check for invalid input range + BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key)); + x = NodeTraits::get_right(x); + } + //If the upper_key is less than x, the target + //range is on the left part + else if(comp(upper_key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else{ + //x is inside the bounded range( x >= lower_key && x <= upper_key), + //so we must split lower and upper searches + // + //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false + BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key)); + return std::pair( + left_closed + //If left_closed, then comp(x, lower_key) is already the lower_bound + //condition so we save one comparison and go to the next level + //following traditional lower_bound algo + ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp) + //If left-open, comp(x, lower_key) is not the upper_bound algo + //condition so we must recheck current 'x' node with upper_bound algo + : upper_bound_loop(x, y, lower_key, comp) + , + right_closed + //If right_closed, then comp(upper_key, x) is already the upper_bound + //condition so we can save one comparison and go to the next level + //following lower_bound algo + ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp) + //If right-open, comp(upper_key, x) is not the lower_bound algo + //condition so we must recheck current 'x' node with lower_bound algo + : lower_bound_loop(x, y, upper_key, comp) + ); + } + } + return std::pair (y, y); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns the number of elements with a key equivalent to "key" + //! according to "comp". + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::size_t count + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + std::pair ret = equal_range(header, key, comp); + std::size_t n = 0; + while(ret.first != ret.second){ + ++n; + ret.first = next_node(ret.first); + } + return n; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an a pair of node_ptr delimiting a range containing + //! all elements that are equivalent to "key" according to "comp" or an + //! empty range that indicates the position where those elements would be + //! if there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair equal_range + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return bounded_range(header, key, key, comp, true, true); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns an a pair of node_ptr delimiting a range containing + //! the first element that is equivalent to "key" according to "comp" or an + //! empty range that indicates the position where that element would be + //! if there are no equivalent elements. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static std::pair lower_bound_range + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + node_ptr const lb(lower_bound(header, key, comp)); + std::pair ret_ii(lb, lb); + if(lb != header && !comp(key, lb)){ + ret_ii.second = next_node(ret_ii.second); + } + return ret_ii; + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns a node_ptr to the first element that is + //! not less than "key" according to "comp" or "header" if that element does + //! not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr lower_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs. + //! + //! Effects: Returns a node_ptr to the first element that is greater + //! than "key" according to "comp" or "header" if that element does not exist. + //! + //! Complexity: Logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr upper_bound + (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp) + { + return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp); + } + + //! Requires: "header" must be the header node of a tree. + //! "commit_data" must have been obtained from a previous call to + //! "insert_unique_check". No objects should have been inserted or erased + //! from the set between the "insert_unique_check" that filled "commit_data" + //! and the call to "insert_commit". + //! + //! + //! Effects: Inserts new_node in the set using the information obtained + //! from the "commit_data" that a previous "insert_check" filled. + //! + //! Complexity: Constant time. + //! + //! Throws: Nothing. + //! + //! Notes: This function has only sense if a "insert_unique_check" has been + //! previously executed to fill "commit_data". No value should be inserted or + //! erased between the "insert_check" and "insert_commit" calls. + static void insert_unique_commit + (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data) + { return insert_commit(header, new_value, commit_data); } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares KeyType with a node_ptr. + //! + //! Effects: Checks if there is an equivalent node to "key" in the + //! tree according to "comp" and obtains the needed information to realize + //! a constant-time node insertion if there is no equivalent node. + //! + //! Returns: If there is an equivalent value + //! returns a pair containing a node_ptr to the already present node + //! and false. If there is not equivalent key can be inserted returns true + //! in the returned pair's boolean and fills "commit_data" that is meant to + //! be used with the "insert_commit" function to achieve a constant-time + //! insertion function. + //! + //! Complexity: Average complexity is at most logarithmic. + //! + //! Throws: If "comp" throws. + //! + //! Notes: This function is used to improve performance when constructing + //! a node is expensive and the user does not want to have two equivalent nodes + //! in the tree: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the node and this function offers the possibility to use that part + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the node and use + //! "insert_commit" to insert the node in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_unique_commit" only + //! if no more objects are inserted or erased from the set. + template + static std::pair insert_unique_check + (const const_node_ptr & header, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + std::size_t depth = 0; + node_ptr h(detail::uncast(header)); + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + node_ptr prev = node_ptr(); + + //Find the upper bound, cache the previous value and if we should + //store it in the left or right node + bool left_child = true; + while(x){ + ++depth; + y = x; + x = (left_child = comp(key, x)) ? + NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x)); + } + + if(pdepth) *pdepth = depth; + + //Since we've found the upper bound there is no other value with the same key if: + // - There is no previous node + // - The previous node is less than the key + const bool not_present = !prev || comp(prev, key); + if(not_present){ + commit_data.link_left = left_child; + commit_data.node = y; + } + return std::pair(prev, not_present); + } + + //! Requires: "header" must be the header node of a tree. + //! KeyNodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares KeyType with a node_ptr. + //! "hint" is node from the "header"'s tree. + //! + //! Effects: Checks if there is an equivalent node to "key" in the + //! tree according to "comp" using "hint" as a hint to where it should be + //! inserted and obtains the needed information to realize + //! a constant-time node insertion if there is no equivalent node. + //! If "hint" is the upper_bound the function has constant time + //! complexity (two comparisons in the worst case). + //! + //! Returns: If there is an equivalent value + //! returns a pair containing a node_ptr to the already present node + //! and false. If there is not equivalent key can be inserted returns true + //! in the returned pair's boolean and fills "commit_data" that is meant to + //! be used with the "insert_commit" function to achieve a constant-time + //! insertion function. + //! + //! Complexity: Average complexity is at most logarithmic, but it is + //! amortized constant time if new_node should be inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + //! + //! Notes: This function is used to improve performance when constructing + //! a node is expensive and the user does not want to have two equivalent nodes + //! in the tree: if there is an equivalent value + //! the constructed object must be discarded. Many times, the part of the + //! node that is used to impose the order is much cheaper to construct + //! than the node and this function offers the possibility to use that part + //! to check if the insertion will be successful. + //! + //! If the check is successful, the user can construct the node and use + //! "insert_commit" to insert the node in constant-time. This gives a total + //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)). + //! + //! "commit_data" remains valid for a subsequent "insert_unique_commit" only + //! if no more objects are inserted or erased from the set. + template + static std::pair insert_unique_check + (const const_node_ptr & header, const node_ptr &hint, const KeyType &key + ,KeyNodePtrCompare comp, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + //hint must be bigger than the key + if(hint == header || comp(key, hint)){ + node_ptr prev(hint); + //Previous value should be less than the key + if(hint == begin_node(header) || comp((prev = prev_node(hint)), key)){ + commit_data.link_left = unique(header) || !NodeTraits::get_left(hint); + commit_data.node = commit_data.link_left ? hint : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + return std::pair(node_ptr(), true); + } + } + //Hint was wrong, use hintless insertion + return insert_unique_check(header, key, comp, commit_data, pdepth); + } + + //! Requires: "header" must be the header node of a tree. + //! NodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from + //! the "header"'s tree. + //! + //! Effects: Inserts new_node into the tree, using "hint" as a hint to + //! where it will be inserted. If "hint" is the upper_bound + //! the insertion takes constant time (two comparisons in the worst case). + //! + //! Complexity: Logarithmic in general, but it is amortized + //! constant time if new_node is inserted immediately before "hint". + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal + (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_check(h, hint, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "h" must be the header node of a tree. + //! NodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares two node_ptrs. + //! + //! Effects: Inserts new_node into the tree before the upper bound + //! according to "comp". + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal_upper_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "h" must be the header node of a tree. + //! NodePtrCompare is a function object that induces a strict weak + //! ordering compatible with the strict weak ordering used to create the + //! the tree. NodePtrCompare compares two node_ptrs. + //! + //! Effects: Inserts new_node into the tree before the lower bound + //! according to "comp". + //! + //! Complexity: Average complexity for insert element is at + //! most logarithmic. + //! + //! Throws: If "comp" throws. + template + static node_ptr insert_equal_lower_bound + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth); + insert_commit(h, new_node, commit_data); + return new_node; + } + + //! Requires: "header" must be the header node of a tree. + //! "pos" must be a valid iterator or header (end) node. + //! "pos" must be an iterator pointing to the successor to "new_node" + //! once inserted according to the order of already inserted nodes. This function does not + //! check "pos" and this precondition must be guaranteed by the caller. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "pos" is not the successor of the newly inserted "new_node" + //! tree invariants might be broken. + static node_ptr insert_before + (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + insert_before_check(header, pos, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + return new_node; + } + + //! Requires: "header" must be the header node of a tree. + //! "new_node" must be, according to the used ordering no less than the + //! greatest inserted key. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "new_node" is less than the greatest inserted key + //! tree invariants are broken. This function is slightly faster than + //! using "insert_before". + static void push_back + (const node_ptr & header, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + push_back_check(header, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + } + + //! Requires: "header" must be the header node of a tree. + //! "new_node" must be, according to the used ordering, no greater than the + //! lowest inserted key. + //! + //! Effects: Inserts new_node into the tree before "pos". + //! + //! Complexity: Constant-time. + //! + //! Throws: Nothing. + //! + //! Note: If "new_node" is greater than the lowest inserted key + //! tree invariants are broken. This function is slightly faster than + //! using "insert_before". + static void push_front + (const node_ptr & header, const node_ptr & new_node + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + insert_commit_data commit_data; + push_front_check(header, commit_data, pdepth); + insert_commit(header, new_node, commit_data); + } + + //! Requires: 'node' can't be a header node. + //! + //! Effects: Calculates the depth of a node: the depth of a + //! node is the length (number of edges) of the path from the root + //! to that node. (The root node is at depth 0.) + //! + //! Complexity: Logarithmic to the number of nodes in the tree. + //! + //! Throws: Nothing. + static std::size_t depth(const_node_ptr node) + { + std::size_t depth = 0; + node_ptr p_parent; + while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){ + ++depth; + node = p_parent; + } + return depth; + } + + //! Requires: "cloner" must be a function + //! object taking a node_ptr and returning a new cloned node of it. "disposer" must + //! take a node_ptr and shouldn't throw. + //! + //! Effects: First empties target tree calling + //! void disposer::operator()(const node_ptr &) for every node of the tree + //! except the header. + //! + //! Then, duplicates the entire tree pointed by "source_header" cloning each + //! source node with node_ptr Cloner::operator()(const node_ptr &) to obtain + //! the nodes of the target tree. If "cloner" throws, the cloned target nodes + //! are disposed using void disposer(const node_ptr &). + //! + //! Complexity: Linear to the number of element of the source tree plus the. + //! number of elements of tree target tree when calling this function. + //! + //! Throws: If cloner functor throws. If this happens target nodes are disposed. + template + static void clone + (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer) + { + if(!unique(target_header)){ + clear_and_dispose(target_header, disposer); + } + + node_ptr leftmost, rightmost; + node_ptr new_root = clone_subtree + (source_header, target_header, cloner, disposer, leftmost, rightmost); + + //Now update header node + NodeTraits::set_parent(target_header, new_root); + NodeTraits::set_left (target_header, leftmost); + NodeTraits::set_right (target_header, rightmost); + } + + //! Requires: header must be the header of a tree, z a node + //! of that tree and z != header. + //! + //! Effects: Erases node "z" from the tree with header "header". + //! + //! Complexity: Amortized constant time. + //! + //! Throws: Nothing. + static void erase(const node_ptr & header, const node_ptr & z) + { + data_for_rebalance ignored; + erase(header, z, ignored); + } + + //! Requires: node is a tree node but not the header. + //! + //! Effects: Unlinks the node and rebalances the tree. + //! + //! Complexity: Average complexity is constant time. + //! + //! Throws: Nothing. + static void unlink(const node_ptr & node) + { + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)) + x = NodeTraits::get_parent(x); + erase(x, node); + } + } + + //! Requires: header must be the header of a tree. + //! + //! Effects: Rebalances the tree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + static void rebalance(const node_ptr & header) + { + node_ptr root = NodeTraits::get_parent(header); + if(root){ + rebalance_subtree(root); + } + } + + //! Requires: old_root is a node of a tree. It shall not be null. + //! + //! Effects: Rebalances the subtree rooted at old_root. + //! + //! Returns: The new root of the subtree. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear. + static node_ptr rebalance_subtree(const node_ptr & old_root) + { + //Taken from: + //"Tree rebalancing in optimal time and space" + //Quentin F. Stout and Bette L. Warren + + //To avoid irregularities in the algorithm (old_root can be a + //left or right child or even the root of the tree) just put the + //root as the right child of its parent. Before doing this backup + //information to restore the original relationship after + //the algorithm is applied. + node_ptr super_root = NodeTraits::get_parent(old_root); + BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root); + + //Get root info + node_ptr super_root_right_backup = NodeTraits::get_right(super_root); + bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root; + bool old_root_is_right = is_right_child(old_root); + NodeTraits::set_right(super_root, old_root); + + std::size_t size; + subtree_to_vine(super_root, size); + vine_to_subtree(super_root, size); + node_ptr new_root = NodeTraits::get_right(super_root); + + //Recover root + if(super_root_is_header){ + NodeTraits::set_right(super_root, super_root_right_backup); + NodeTraits::set_parent(super_root, new_root); + } + else if(old_root_is_right){ + NodeTraits::set_right(super_root, new_root); + } + else{ + NodeTraits::set_right(super_root, super_root_right_backup); + NodeTraits::set_left(super_root, new_root); + } + return new_root; + } + + //! Effects: Asserts the integrity of the container with additional checks provided by the user. + //! + //! Requires: header must be the header of a tree. + //! + //! Complexity: Linear time. + //! + //! Note: The method might not have effect when asserts are turned off (e.g., with NDEBUG). + //! Experimental function, interface might change in future versions. + template + static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return) + { + const_node_ptr root_node_ptr = NodeTraits::get_parent(header); + if (!root_node_ptr) + { + // check left&right header pointers + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header); + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header); + } + else + { + // check parent pointer of root node + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header); + // check subtree from root + check_subtree(root_node_ptr, checker, checker_return); + // check left&right header pointers + const_node_ptr p = root_node_ptr; + while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); } + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p); + p = root_node_ptr; + while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); } + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p); + } + } + + protected: + static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info) + { + node_ptr y(z); + node_ptr x; + const node_ptr z_left(NodeTraits::get_left(z)); + const node_ptr z_right(NodeTraits::get_right(z)); + + if(!z_left){ + x = z_right; // x might be null. + } + else if(!z_right){ // z has exactly one non-null child. y == z. + x = z_left; // x is not null. + BOOST_ASSERT(x); + } + else{ //make y != z + // y = find z's successor + y = bstree_algorithms::minimum(z_right); + x = NodeTraits::get_right(y); // x might be null. + } + + node_ptr x_parent; + const node_ptr z_parent(NodeTraits::get_parent(z)); + const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z); + + if(y != z){ //has two children and y is the minimum of z + //y is z's successor and it has a null left child. + //x is the right child of y (it can be null) + //Relink y in place of z and link x with y's old parent + NodeTraits::set_parent(z_left, y); + NodeTraits::set_left(y, z_left); + if(y != z_right){ + //Link y with the right tree of z + NodeTraits::set_right(y, z_right); + NodeTraits::set_parent(z_right, y); + //Link x with y's old parent (y must be a left child) + x_parent = NodeTraits::get_parent(y); + BOOST_ASSERT(NodeTraits::get_left(x_parent) == y); + if(x) + NodeTraits::set_parent(x, x_parent); + //Since y was the successor and not the right child of z, it must be a left child + NodeTraits::set_left(x_parent, x); + } + else{ //y was the right child of y so no need to fix x's position + x_parent = y; + } + NodeTraits::set_parent(y, z_parent); + bstree_algorithms::set_child(header, y, z_parent, z_is_leftchild); + } + else { // z has zero or one child, x is one child (it can be null) + //Just link x to z's parent + x_parent = z_parent; + if(x) + NodeTraits::set_parent(x, z_parent); + bstree_algorithms::set_child(header, x, z_parent, z_is_leftchild); + + //Now update leftmost/rightmost in case z was one of them + if(NodeTraits::get_left(header) == z){ + //z_left must be null because z is the leftmost + BOOST_ASSERT(!z_left); + NodeTraits::set_left(header, !z_right ? + z_parent : // makes leftmost == header if z == root + bstree_algorithms::minimum(z_right)); + } + if(NodeTraits::get_right(header) == z){ + //z_right must be null because z is the rightmost + BOOST_ASSERT(!z_right); + NodeTraits::set_right(header, !z_left ? + z_parent : // makes rightmost == header if z == root + bstree_algorithms::maximum(z_left)); + } + } + + //If z had 0/1 child, y == z and one of its children (and maybe null) + //If z had 2 children, y is the successor of z and x is the right child of y + info.x = x; + info.y = y; + //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor) + //If z had 2 children, x_parent is the new parent of y (z_parent) + BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent); + info.x_parent = x_parent; + } + + //! Requires: node is a node of the tree but it's not the header. + //! + //! Effects: Returns the number of nodes of the subtree. + //! + //! Complexity: Linear time. + //! + //! Throws: Nothing. + static std::size_t subtree_size(const const_node_ptr & subtree) + { + std::size_t count = 0; + if (subtree){ + node_ptr n = detail::uncast(subtree); + node_ptr m = NodeTraits::get_left(n); + while(m){ + n = m; + m = NodeTraits::get_left(n); + } + + while(1){ + ++count; + node_ptr n_right(NodeTraits::get_right(n)); + if(n_right){ + n = n_right; + m = NodeTraits::get_left(n); + while(m){ + n = m; + m = NodeTraits::get_left(n); + } + } + else { + do{ + if (n == subtree){ + return count; + } + m = n; + n = NodeTraits::get_parent(n); + }while(NodeTraits::get_left(n) != m); + } + } + } + return count; + } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a left child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_left_child(const node_ptr & p) + { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; } + + //! Requires: p is a node of a tree. + //! + //! Effects: Returns true if p is a right child. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool is_right_child(const node_ptr & p) + { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; } + + static void insert_before_check + (const node_ptr &header, const node_ptr & pos + , insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr prev(pos); + if(pos != NodeTraits::get_left(header)) + prev = prev_node(pos); + bool link_left = unique(header) || !NodeTraits::get_left(pos); + commit_data.link_left = link_left; + commit_data.node = link_left ? pos : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + } + + static void push_back_check + (const node_ptr & header, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr prev(NodeTraits::get_right(header)); + if(pdepth){ + *pdepth = prev == header ? 0 : depth(prev) + 1; + } + commit_data.link_left = false; + commit_data.node = prev; + } + + static void push_front_check + (const node_ptr & header, insert_commit_data &commit_data + #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + , std::size_t *pdepth = 0 + #endif + ) + { + node_ptr pos(NodeTraits::get_left(header)); + if(pdepth){ + *pdepth = pos == header ? 0 : depth(pos) + 1; + } + commit_data.link_left = true; + commit_data.node = pos; + } + + template + static void insert_equal_check + (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp + , insert_commit_data &commit_data + /// @cond + , std::size_t *pdepth = 0 + /// @endcond + ) + { + if(hint == header || !comp(hint, new_node)){ + node_ptr prev(hint); + if(hint == NodeTraits::get_left(header) || + !comp(new_node, (prev = prev_node(hint)))){ + bool link_left = unique(header) || !NodeTraits::get_left(hint); + commit_data.link_left = link_left; + commit_data.node = link_left ? hint : prev; + if(pdepth){ + *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1; + } + } + else{ + insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth); + } + } + else{ + insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth); + } + } + + template + static void insert_equal_upper_bound_check + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) + { + std::size_t depth = 0; + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + ++depth; + y = x; + x = comp(new_node, x) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + if(pdepth) *pdepth = depth; + commit_data.link_left = (y == h) || comp(new_node, y); + commit_data.node = y; + } + + template + static void insert_equal_lower_bound_check + (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0) + { + std::size_t depth = 0; + node_ptr y(h); + node_ptr x(NodeTraits::get_parent(y)); + + while(x){ + ++depth; + y = x; + x = !comp(x, new_node) ? + NodeTraits::get_left(x) : NodeTraits::get_right(x); + } + if(pdepth) *pdepth = depth; + commit_data.link_left = (y == h) || !comp(y, new_node); + commit_data.node = y; + } + + static void insert_commit + (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data) + { + //Check if commit_data has not been initialized by a insert_unique_check call. + BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr()); + node_ptr parent_node(commit_data.node); + if(parent_node == header){ + NodeTraits::set_parent(header, new_node); + NodeTraits::set_right(header, new_node); + NodeTraits::set_left(header, new_node); + } + else if(commit_data.link_left){ + NodeTraits::set_left(parent_node, new_node); + if(parent_node == NodeTraits::get_left(header)) + NodeTraits::set_left(header, new_node); + } + else{ + NodeTraits::set_right(parent_node, new_node); + if(parent_node == NodeTraits::get_right(header)) + NodeTraits::set_right(header, new_node); + } + NodeTraits::set_parent(new_node, parent_node); + NodeTraits::set_right(new_node, node_ptr()); + NodeTraits::set_left(new_node, node_ptr()); + } + + //Fix header and own's parent data when replacing x with own, providing own's old data with parent + static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left) + { + if(new_parent == header) + NodeTraits::set_parent(header, new_child); + else if(link_left) + NodeTraits::set_left(new_parent, new_child); + else + NodeTraits::set_right(new_parent, new_child); + } + + // rotate p to left (no header and p's parent fixup) + static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right) + { + node_ptr p_right_left(NodeTraits::get_left(p_right)); + NodeTraits::set_right(p, p_right_left); + if(p_right_left){ + NodeTraits::set_parent(p_right_left, p); + } + NodeTraits::set_left(p_right, p); + NodeTraits::set_parent(p, p_right); + } + + // rotate p to left (with header and p's parent fixup) + static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header) + { + const bool p_was_left(NodeTraits::get_left(p_parent) == p); + rotate_left_no_parent_fix(p, p_right); + NodeTraits::set_parent(p_right, p_parent); + set_child(header, p_right, p_parent, p_was_left); + } + + // rotate p to right (no header and p's parent fixup) + static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left) + { + node_ptr p_left_right(NodeTraits::get_right(p_left)); + NodeTraits::set_left(p, p_left_right); + if(p_left_right){ + NodeTraits::set_parent(p_left_right, p); + } + NodeTraits::set_right(p_left, p); + NodeTraits::set_parent(p, p_left); + } + + // rotate p to right (with header and p's parent fixup) + static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header) + { + const bool p_was_left(NodeTraits::get_left(p_parent) == p); + rotate_right_no_parent_fix(p, p_left); + NodeTraits::set_parent(p_left, p_parent); + set_child(header, p_left, p_parent, p_was_left); + } + + private: + + static void subtree_to_vine(node_ptr vine_tail, std::size_t &size) + { + //Inspired by LibAVL: + //It uses a clever optimization for trees with parent pointers. + //No parent pointer is updated when transforming a tree to a vine as + //most of them will be overriten during compression rotations. + //A final pass must be made after the rebalancing to updated those + //pointers not updated by tree_to_vine + compression calls + std::size_t len = 0; + node_ptr remainder = NodeTraits::get_right(vine_tail); + while(remainder){ + node_ptr tempptr = NodeTraits::get_left(remainder); + if(!tempptr){ //move vine-tail down one + vine_tail = remainder; + remainder = NodeTraits::get_right(remainder); + ++len; + } + else{ //rotate + NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr)); + NodeTraits::set_right(tempptr, remainder); + remainder = tempptr; + NodeTraits::set_right(vine_tail, tempptr); + } + } + size = len; + } + + static void compress_subtree(node_ptr scanner, std::size_t count) + { + while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner + node_ptr child = NodeTraits::get_right(scanner); + node_ptr child_right = NodeTraits::get_right(child); + NodeTraits::set_right(scanner, child_right); + //Avoid setting the parent of child_right + scanner = child_right; + node_ptr scanner_left = NodeTraits::get_left(scanner); + NodeTraits::set_right(child, scanner_left); + if(scanner_left) + NodeTraits::set_parent(scanner_left, child); + NodeTraits::set_left(scanner, child); + NodeTraits::set_parent(child, scanner); + } + } + + static void vine_to_subtree(const node_ptr & super_root, std::size_t count) + { + const std::size_t one_szt = 1u; + std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt)); + compress_subtree(super_root, leaf_nodes); //create deepest leaves + std::size_t vine_nodes = count - leaf_nodes; + while(vine_nodes > 1){ + vine_nodes /= 2; + compress_subtree(super_root, vine_nodes); + } + + //Update parents of nodes still in the in the original vine line + //as those have not been updated by subtree_to_vine or compress_subtree + for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root) + ; p + ; q = p, p = NodeTraits::get_right(p)){ + NodeTraits::set_parent(p, q); + } + } + + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_root(const node_ptr & node) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node))); + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)){ + x = NodeTraits::get_parent(x); + } + return x; + } + else{ + return node; + } + } + + template + static node_ptr clone_subtree + (const const_node_ptr &source_parent, const node_ptr &target_parent + , Cloner cloner, Disposer disposer + , node_ptr &leftmost_out, node_ptr &rightmost_out + ) + { + node_ptr target_sub_root = target_parent; + node_ptr source_root = NodeTraits::get_parent(source_parent); + if(!source_root){ + leftmost_out = rightmost_out = source_root; + } + else{ + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr current = source_root; + node_ptr insertion_point = target_sub_root = cloner(current); + + //We'll calculate leftmost and rightmost nodes while iterating + node_ptr leftmost = target_sub_root; + node_ptr rightmost = target_sub_root; + + //First set the subroot + NodeTraits::set_left(target_sub_root, node_ptr()); + NodeTraits::set_right(target_sub_root, node_ptr()); + NodeTraits::set_parent(target_sub_root, target_parent); + + dispose_subtree_disposer rollback(disposer, target_sub_root); + while(true) { + //First clone left nodes + if( NodeTraits::get_left(current) && + !NodeTraits::get_left(insertion_point)) { + current = NodeTraits::get_left(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr()); + NodeTraits::set_right (insertion_point, node_ptr()); + //Insert left + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_left (temp, insertion_point); + //Update leftmost + if(rightmost == target_sub_root) + leftmost = insertion_point; + } + //Then clone right nodes + else if( NodeTraits::get_right(current) && + !NodeTraits::get_right(insertion_point)){ + current = NodeTraits::get_right(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr()); + NodeTraits::set_right (insertion_point, node_ptr()); + //Insert right + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_right (temp, insertion_point); + //Update rightmost + rightmost = insertion_point; + } + //If not, go up + else if(current == source_root){ + break; + } + else{ + //Branch completed, go up searching more nodes to clone + current = NodeTraits::get_parent(current); + insertion_point = NodeTraits::get_parent(insertion_point); + } + } + rollback.release(); + leftmost_out = leftmost; + rightmost_out = rightmost; + } + return target_sub_root; + } + + template + static void dispose_subtree(node_ptr x, Disposer disposer) + { + while (x){ + node_ptr save(NodeTraits::get_left(x)); + if (save) { + // Right rotation + NodeTraits::set_left(x, NodeTraits::get_right(save)); + NodeTraits::set_right(save, x); + } + else { + save = NodeTraits::get_right(x); + init(x); + disposer(x); + } + x = save; + } + } + + template + static node_ptr lower_bound_loop + (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) + { + while(x){ + if(comp(x, key)){ + x = NodeTraits::get_right(x); + } + else{ + y = x; + x = NodeTraits::get_left(x); + } + } + return y; + } + + template + static node_ptr upper_bound_loop + (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp) + { + while(x){ + if(comp(key, x)){ + y = x; + x = NodeTraits::get_left(x); + } + else{ + x = NodeTraits::get_right(x); + } + } + return y; + } + + template + static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return) + { + const_node_ptr left = NodeTraits::get_left(node); + const_node_ptr right = NodeTraits::get_right(node); + typename Checker::return_type check_return_left; + typename Checker::return_type check_return_right; + if (left) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node); + check_subtree(left, checker, check_return_left); + } + if (right) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node); + check_subtree(right, checker, check_return_right); + } + checker(node, check_return_left, check_return_right, check_return); + } +}; + +/// @cond + +template +struct get_algo +{ + typedef bstree_algorithms type; +}; + +template +struct get_node_checker +{ + typedef detail::bstree_node_checker type; +}; + +/// @endcond + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP diff --git a/boost/intrusive/circular_list_algorithms.hpp b/boost/intrusive/circular_list_algorithms.hpp new file mode 100644 index 0000000..1e888a1 --- /dev/null +++ b/boost/intrusive/circular_list_algorithms.hpp @@ -0,0 +1,507 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! circular_list_algorithms provides basic algorithms to manipulate nodes +//! forming a circular doubly linked list. An empty circular list is formed by a node +//! whose pointers point to itself. +//! +//! circular_list_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the circular list +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_previous(const_node_ptr n); +//! +//! static void set_previous(node_ptr n, node_ptr prev); +//! +//! static node_ptr get_next(const_node_ptr n); +//! +//! static void set_next(node_ptr n, node_ptr next); +template +class circular_list_algorithms +{ + public: + typedef typename NodeTraits::node node; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef NodeTraits node_traits; + + //! Effects: Constructs an non-used list element, so that + //! inited(this_node) == true + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init(const node_ptr &this_node) + { + const node_ptr null_node((node_ptr())); + NodeTraits::set_next(this_node, null_node); + NodeTraits::set_previous(this_node, null_node); + } + + //! Effects: Returns true is "this_node" is in a non-used state + //! as if it was initialized by the "init" function. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const const_node_ptr &this_node) + { return !NodeTraits::get_next(this_node); } + + //! Effects: Constructs an empty list, making this_node the only + //! node of the circular list: + //! NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node) + //! == this_node. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(const node_ptr &this_node) + { + NodeTraits::set_next(this_node, this_node); + NodeTraits::set_previous(this_node, this_node); + } + + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns true is "this_node" is the only node of a circular list: + //! return NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool unique(const const_node_ptr &this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns the number of nodes in a circular list. If the circular list + //! is empty, returns 1. + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t count(const const_node_ptr &this_node) + { + std::size_t result = 0; + const_node_ptr p = this_node; + do{ + p = NodeTraits::get_next(p); + ++result; + }while (p != this_node); + return result; + } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Unlinks the node from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static node_ptr unlink(const node_ptr &this_node) + { + node_ptr next(NodeTraits::get_next(this_node)); + node_ptr prev(NodeTraits::get_previous(this_node)); + NodeTraits::set_next(prev, next); + NodeTraits::set_previous(next, prev); + return next; + } + + //! Requires: b and e must be nodes of the same circular list or an empty range. + //! + //! Effects: Unlinks the node [b, e) from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink(const node_ptr &b, const node_ptr &e) + { + if (b != e) { + node_ptr prevb(NodeTraits::get_previous(b)); + NodeTraits::set_previous(e, prevb); + NodeTraits::set_next(prevb, e); + } + } + + //! Requires: nxt_node must be a node of a circular list. + //! + //! Effects: Links this_node before nxt_node in the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_before(const node_ptr &nxt_node, const node_ptr &this_node) + { + node_ptr prev(NodeTraits::get_previous(nxt_node)); + NodeTraits::set_previous(this_node, prev); + NodeTraits::set_next(this_node, nxt_node); + //nxt_node might be an alias for prev->next_ + //so use it before NodeTraits::set_next(prev, ...) + //is called and the reference changes it's value + NodeTraits::set_previous(nxt_node, this_node); + NodeTraits::set_next(prev, this_node); + } + + //! Requires: prev_node must be a node of a circular list. + //! + //! Effects: Links this_node after prev_node in the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_after(const node_ptr &prev_node, const node_ptr &this_node) + { + node_ptr next(NodeTraits::get_next(prev_node)); + NodeTraits::set_previous(this_node, prev_node); + NodeTraits::set_next(this_node, next); + //prev_node might be an alias for next->next_ + //so use it before update it before NodeTraits::set_previous(next, ...) + //is called and the reference changes it's value + NodeTraits::set_next(prev_node, this_node); + NodeTraits::set_previous(next, this_node); + } + + //! Requires: this_node and other_node must be nodes inserted + //! in circular lists or be empty circular lists. + //! + //! Effects: Swaps the position of the nodes: this_node is inserted in + //! other_nodes position in the second circular list and the other_node is inserted + //! in this_node's position in the first circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. +/* + static void swap_nodes(const node_ptr &this_node, const node_ptr &other_node) + { + + if (other_node == this_node) + return; + bool empty1 = unique(this_node); + bool empty2 = unique(other_node); + + node_ptr next_this(NodeTraits::get_next(this_node)); + node_ptr prev_this(NodeTraits::get_previous(this_node)); + node_ptr next_other(NodeTraits::get_next(other_node)); + node_ptr prev_other(NodeTraits::get_previous(other_node)); + + //Do the swap + NodeTraits::set_next(this_node, next_other); + NodeTraits::set_next(other_node, next_this); + + NodeTraits::set_previous(this_node, prev_other); + NodeTraits::set_previous(other_node, prev_this); + + if (empty2){ + init(this_node); + } + else{ + NodeTraits::set_next(prev_other, this_node); + NodeTraits::set_previous(next_other, this_node); + } + if (empty1){ + init(other_node); + } + else{ + NodeTraits::set_next(prev_this, other_node); + NodeTraits::set_previous(next_this, other_node); + } + } +*/ + + //Watanabe version + private: + static void swap_prev(const node_ptr &this_node, const node_ptr &other_node) + { + node_ptr temp(NodeTraits::get_previous(this_node)); + NodeTraits::set_previous(this_node, NodeTraits::get_previous(other_node)); + NodeTraits::set_previous(other_node, temp); + } + static void swap_next(const node_ptr &this_node, const node_ptr &other_node) + { + node_ptr temp(NodeTraits::get_next(this_node)); + NodeTraits::set_next(this_node, NodeTraits::get_next(other_node)); + NodeTraits::set_next(other_node, temp); + } + + public: + static void swap_nodes(const node_ptr &this_node, const node_ptr &other_node) + { + if (other_node == this_node) + return; + bool this_inited = inited(this_node); + bool other_inited = inited(other_node); + if(this_inited){ + init_header(this_node); + } + if(other_inited){ + init_header(other_node); + } + + node_ptr next_this(NodeTraits::get_next(this_node)); + node_ptr prev_this(NodeTraits::get_previous(this_node)); + node_ptr next_other(NodeTraits::get_next(other_node)); + node_ptr prev_other(NodeTraits::get_previous(other_node)); + //these first two swaps must happen before the other two + swap_prev(next_this, next_other); + swap_next(prev_this, prev_other); + swap_next(this_node, other_node); + swap_prev(this_node, other_node); + + if(this_inited){ + init(other_node); + } + if(other_inited){ + init(this_node); + } + } + + //! Requires: b and e must be nodes of the same circular list or an empty range. + //! and p must be a node of a different circular list or may not be an iterator in + // [b, e). + //! + //! Effects: Removes the nodes from [b, e) range from their circular list and inserts + //! them before p in p's circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer(const node_ptr &p, const node_ptr &b, const node_ptr &e) + { + if (b != e) { + node_ptr prev_p(NodeTraits::get_previous(p)); + node_ptr prev_b(NodeTraits::get_previous(b)); + node_ptr prev_e(NodeTraits::get_previous(e)); + NodeTraits::set_next(prev_e, p); + NodeTraits::set_previous(p, prev_e); + NodeTraits::set_next(prev_b, e); + NodeTraits::set_previous(e, prev_b); + NodeTraits::set_next(prev_p, b); + NodeTraits::set_previous(b, prev_p); + } + } + + //! Requires: i must a node of a circular list + //! and p must be a node of a different circular list. + //! + //! Effects: Removes the node i from its circular list and inserts + //! it before p in p's circular list. + //! If p == i or p == NodeTraits::get_next(i), this function is a null operation. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer(const node_ptr &p, const node_ptr &i) + { + node_ptr n(NodeTraits::get_next(i)); + if(n != p && i != p){ + node_ptr prev_p(NodeTraits::get_previous(p)); + node_ptr prev_i(NodeTraits::get_previous(i)); + NodeTraits::set_next(prev_p, i); + NodeTraits::set_previous(i, prev_p); + NodeTraits::set_next(i, p); + NodeTraits::set_previous(p, i); + NodeTraits::set_previous(n, prev_i); + NodeTraits::set_next(prev_i, n); + + } + } + + //! Effects: Reverses the order of elements in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: This function is linear time. + static void reverse(const node_ptr &p) + { + node_ptr f(NodeTraits::get_next(p)); + node_ptr i(NodeTraits::get_next(f)), e(p); + + while(i != e) { + node_ptr n = i; + i = NodeTraits::get_next(i); + transfer(f, n, i); + f = n; + } + } + + //! Effects: Moves the node p n positions towards the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of moved positions. + static void move_backwards(const node_ptr &p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr first = NodeTraits::get_next(p); + //size() == 0 or 1, nothing to do + if(first == NodeTraits::get_previous(p)) return; + unlink(p); + //Now get the new first node + while(n--){ + first = NodeTraits::get_next(first); + } + link_before(first, p); + } + + //! Effects: Moves the node p n positions towards the beginning of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of moved positions. + static void move_forward(const node_ptr &p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr last = NodeTraits::get_previous(p); + //size() == 0 or 1, nothing to do + if(last == NodeTraits::get_next(p)) return; + + unlink(p); + //Now get the new last node + while(n--){ + last = NodeTraits::get_previous(last); + } + link_after(last, p); + } + + //! Requires: f and l must be in a circular list. + //! + //! Effects: Returns the number of nodes in the range [f, l). + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t distance(const const_node_ptr &f, const const_node_ptr &l) + { + const_node_ptr i(f); + std::size_t result = 0; + while(i != l){ + i = NodeTraits::get_next(i); + ++result; + } + return result; + } + + struct stable_partition_info + { + std::size_t num_1st_partition; + std::size_t num_2nd_partition; + node_ptr beg_2st_partition; + }; + + template + static void stable_partition(node_ptr beg, const node_ptr &end, Pred pred, stable_partition_info &info) + { + node_ptr bcur = node_traits::get_previous(beg); + node_ptr cur = beg; + node_ptr new_f = end; + + std::size_t num1 = 0, num2 = 0; + while(cur != end){ + if(pred(cur)){ + ++num1; + bcur = cur; + cur = node_traits::get_next(cur); + } + else{ + ++num2; + node_ptr last_to_remove = bcur; + new_f = cur; + bcur = cur; + cur = node_traits::get_next(cur); + BOOST_TRY{ + //Main loop + while(cur != end){ + if(pred(cur)){ //Might throw + ++num1; + //Process current node + node_traits::set_next (last_to_remove, cur); + node_traits::set_previous(cur, last_to_remove); + last_to_remove = cur; + node_ptr nxt = node_traits::get_next(cur); + node_traits::set_next (bcur, nxt); + node_traits::set_previous(nxt, bcur); + cur = nxt; + } + else{ + ++num2; + bcur = cur; + cur = node_traits::get_next(cur); + } + } + } + BOOST_CATCH(...){ + node_traits::set_next (last_to_remove, new_f); + node_traits::set_previous(new_f, last_to_remove); + throw; + } + BOOST_CATCH_END + node_traits::set_next(last_to_remove, new_f); + node_traits::set_previous(new_f, last_to_remove); + break; + } + } + info.num_1st_partition = num1; + info.num_2nd_partition = num2; + info.beg_2st_partition = new_f; + } +}; + +/// @cond + +template +struct get_algo +{ + typedef circular_list_algorithms type; +}; + +/// @endcond + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP diff --git a/boost/intrusive/circular_slist_algorithms.hpp b/boost/intrusive/circular_slist_algorithms.hpp new file mode 100644 index 0000000..3dd561d --- /dev/null +++ b/boost/intrusive/circular_slist_algorithms.hpp @@ -0,0 +1,407 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include + + +namespace boost { +namespace intrusive { + +//! circular_slist_algorithms provides basic algorithms to manipulate nodes +//! forming a circular singly linked list. An empty circular list is formed by a node +//! whose pointer to the next node points to itself. +//! +//! circular_slist_algorithms is configured with a NodeTraits class, which encapsulates the +//! information about the node to be manipulated. NodeTraits must support the +//! following interface: +//! +//! Typedefs: +//! +//! node: The type of the node that forms the circular list +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_next(const_node_ptr n); +//! +//! static void set_next(node_ptr n, node_ptr next); +template +class circular_slist_algorithms + /// @cond + : public detail::common_slist_algorithms + /// @endcond +{ + /// @cond + typedef detail::common_slist_algorithms base_t; + /// @endcond + public: + typedef typename NodeTraits::node node; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef NodeTraits node_traits; + + #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Constructs an non-used list element, putting the next + //! pointer to null: + //! NodeTraits::get_next(this_node) == node_ptr() + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init(node_ptr this_node); + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns true is "this_node" is the only node of a circular list: + //! or it's a not inserted node: + //! return node_ptr() == NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool unique(const_node_ptr this_node); + + //! Effects: Returns true is "this_node" has the same state as + //! if it was inited using "init(node_ptr)" + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node); + + //! Requires: prev_node must be in a circular list or be an empty circular list. + //! + //! Effects: Unlinks the next node of prev_node from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node); + + //! Requires: prev_node and last_node must be in a circular list + //! or be an empty circular list. + //! + //! Effects: Unlinks the range (prev_node, last_node) from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node, node_ptr last_node); + + //! Requires: prev_node must be a node of a circular list. + //! + //! Effects: Links this_node after prev_node in the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_after(node_ptr prev_node, node_ptr this_node); + + //! Requires: b and e must be nodes of the same circular list or an empty range. + //! and p must be a node of a different circular list. + //! + //! Effects: Removes the nodes from (b, e] range from their circular list and inserts + //! them after p in p's circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer_after(node_ptr p, node_ptr b, node_ptr e); + + #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) + + //! Effects: Constructs an empty list, making this_node the only + //! node of the circular list: + //! NodeTraits::get_next(this_node) == this_node. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(const node_ptr &this_node) + { NodeTraits::set_next(this_node, this_node); } + + //! Requires: this_node and prev_init_node must be in the same circular list. + //! + //! Effects: Returns the previous node of this_node in the circular list starting. + //! the search from prev_init_node. The first node checked for equality + //! is NodeTraits::get_next(prev_init_node). + //! + //! Complexity: Linear to the number of elements between prev_init_node and this_node. + //! + //! Throws: Nothing. + static node_ptr get_previous_node(const node_ptr &prev_init_node, const node_ptr &this_node) + { return base_t::get_previous_node(prev_init_node, this_node); } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns the previous node of this_node in the circular list. + //! + //! Complexity: Linear to the number of elements in the circular list. + //! + //! Throws: Nothing. + static node_ptr get_previous_node(const node_ptr & this_node) + { return base_t::get_previous_node(this_node, this_node); } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns the previous node of the previous node of this_node in the circular list. + //! + //! Complexity: Linear to the number of elements in the circular list. + //! + //! Throws: Nothing. + static node_ptr get_previous_previous_node(const node_ptr & this_node) + { return get_previous_previous_node(this_node, this_node); } + + //! Requires: this_node and p must be in the same circular list. + //! + //! Effects: Returns the previous node of the previous node of this_node in the + //! circular list starting. the search from p. The first node checked + //! for equality is NodeTraits::get_next((NodeTraits::get_next(p)). + //! + //! Complexity: Linear to the number of elements in the circular list. + //! + //! Throws: Nothing. + static node_ptr get_previous_previous_node(node_ptr p, const node_ptr & this_node) + { + node_ptr p_next = NodeTraits::get_next(p); + node_ptr p_next_next = NodeTraits::get_next(p_next); + while (this_node != p_next_next){ + p = p_next; + p_next = p_next_next; + p_next_next = NodeTraits::get_next(p_next); + } + return p; + } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns the number of nodes in a circular list. If the circular list + //! is empty, returns 1. + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t count(const const_node_ptr & this_node) + { + std::size_t result = 0; + const_node_ptr p = this_node; + do{ + p = NodeTraits::get_next(p); + ++result; + } while (p != this_node); + return result; + } + + //! Requires: this_node must be in a circular list, be an empty circular list or be inited. + //! + //! Effects: Unlinks the node from the circular list. + //! + //! Complexity: Linear to the number of elements in the circular list + //! + //! Throws: Nothing. + static void unlink(const node_ptr & this_node) + { + if(NodeTraits::get_next(this_node)) + base_t::unlink_after(get_previous_node(this_node)); + } + + //! Requires: nxt_node must be a node of a circular list. + //! + //! Effects: Links this_node before nxt_node in the circular list. + //! + //! Complexity: Linear to the number of elements in the circular list. + //! + //! Throws: Nothing. + static void link_before (const node_ptr & nxt_node, const node_ptr & this_node) + { base_t::link_after(get_previous_node(nxt_node), this_node); } + + //! Requires: this_node and other_node must be nodes inserted + //! in circular lists or be empty circular lists. + //! + //! Effects: Swaps the position of the nodes: this_node is inserted in + //! other_nodes position in the second circular list and the other_node is inserted + //! in this_node's position in the first circular list. + //! + //! Complexity: Linear to number of elements of both lists + //! + //! Throws: Nothing. + static void swap_nodes(const node_ptr & this_node, const node_ptr & other_node) + { + if (other_node == this_node) + return; + const node_ptr this_next = NodeTraits::get_next(this_node); + const node_ptr other_next = NodeTraits::get_next(other_node); + const bool this_null = !this_next; + const bool other_null = !other_next; + const bool this_empty = this_next == this_node; + const bool other_empty = other_next == other_node; + + if(!(other_null || other_empty)){ + NodeTraits::set_next(this_next == other_node ? other_node : get_previous_node(other_node), this_node ); + } + if(!(this_null | this_empty)){ + NodeTraits::set_next(other_next == this_node ? this_node : get_previous_node(this_node), other_node ); + } + NodeTraits::set_next(this_node, other_empty ? this_node : (other_next == this_node ? other_node : other_next) ); + NodeTraits::set_next(other_node, this_empty ? other_node : (this_next == other_node ? this_node : this_next ) ); + } + + //! Effects: Reverses the order of elements in the list. + //! + //! Throws: Nothing. + //! + //! Complexity: This function is linear to the contained elements. + static void reverse(const node_ptr & p) + { + node_ptr i = NodeTraits::get_next(p), e(p); + for (;;) { + node_ptr nxt(NodeTraits::get_next(i)); + if (nxt == e) + break; + base_t::transfer_after(e, i, nxt); + } + } + + //! Effects: Moves the node p n positions towards the end of the list. + //! + //! Returns: The previous node of p after the function if there has been any movement, + //! Null if n leads to no movement. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static node_ptr move_backwards(const node_ptr & p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return node_ptr(); + node_ptr first = NodeTraits::get_next(p); + + //count() == 1 or 2, nothing to do + if(NodeTraits::get_next(first) == p) + return node_ptr(); + + bool end_found = false; + node_ptr new_last = node_ptr(); + + //Now find the new last node according to the shift count. + //If we find p before finding the new last node + //unlink p, shortcut the search now that we know the size of the list + //and continue. + for(std::size_t i = 1; i <= n; ++i){ + new_last = first; + first = NodeTraits::get_next(first); + if(first == p){ + //Shortcut the shift with the modulo of the size of the list + n %= i; + if(!n) + return node_ptr(); + i = 0; + //Unlink p and continue the new first node search + first = NodeTraits::get_next(p); + base_t::unlink_after(new_last); + end_found = true; + } + } + + //If the p has not been found in the previous loop, find it + //starting in the new first node and unlink it + if(!end_found){ + base_t::unlink_after(base_t::get_previous_node(first, p)); + } + + //Now link p after the new last node + base_t::link_after(new_last, p); + return new_last; + } + + //! Effects: Moves the node p n positions towards the beginning of the list. + //! + //! Returns: The previous node of p after the function if there has been any movement, + //! Null if n leads equals to no movement. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static node_ptr move_forward(const node_ptr & p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return node_ptr(); + node_ptr first = node_traits::get_next(p); + + //count() == 1 or 2, nothing to do + if(node_traits::get_next(first) == p) return node_ptr(); + + //Iterate until p is found to know where the current last node is. + //If the shift count is less than the size of the list, we can also obtain + //the position of the new last node after the shift. + node_ptr old_last(first), next_to_it, new_last(p); + std::size_t distance = 1; + while(p != (next_to_it = node_traits::get_next(old_last))){ + if(++distance > n) + new_last = node_traits::get_next(new_last); + old_last = next_to_it; + } + //If the shift was bigger or equal than the size, obtain the equivalent + //forward shifts and find the new last node. + if(distance <= n){ + //Now find the equivalent forward shifts. + //Shortcut the shift with the modulo of the size of the list + std::size_t new_before_last_pos = (distance - (n % distance))% distance; + //If the shift is a multiple of the size there is nothing to do + if(!new_before_last_pos) return node_ptr(); + + for( new_last = p + ; new_before_last_pos-- + ; new_last = node_traits::get_next(new_last)){ + //empty + } + } + + //Now unlink p and link it after the new last node + base_t::unlink_after(old_last); + base_t::link_after(new_last, p); + return new_last; + } +}; + +/// @cond + +template +struct get_algo +{ + typedef circular_slist_algorithms type; +}; + +/// @endcond + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP diff --git a/boost/intrusive/derivation_value_traits.hpp b/boost/intrusive/derivation_value_traits.hpp new file mode 100644 index 0000000..87bc489 --- /dev/null +++ b/boost/intrusive/derivation_value_traits.hpp @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP +#define BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +//!This value traits template is used to create value traits +//!from user defined node traits where value_traits::value_type will +//!derive from node_traits::node + +template +struct derivation_value_traits +{ + public: + typedef NodeTraits node_traits; + typedef T value_type; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + typedef typename boost::intrusive:: + pointer_traits::reference reference; + typedef typename boost::intrusive:: + pointer_traits::reference const_reference; + static const link_mode_type link_mode = LinkMode; + + static node_ptr to_node_ptr(reference value) + { return node_ptr(&value); } + + static const_node_ptr to_node_ptr(const_reference value) + { return node_ptr(&value); } + + static pointer to_value_ptr(const node_ptr &n) + { + return pointer_traits::pointer_to(static_cast(*n)); + } + + static const_pointer to_value_ptr(const const_node_ptr &n) + { + return pointer_traits::pointer_to(static_cast(*n)); + } +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/algo_type.hpp b/boost/intrusive/detail/algo_type.hpp new file mode 100644 index 0000000..e40cb43 --- /dev/null +++ b/boost/intrusive/detail/algo_type.hpp @@ -0,0 +1,46 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_ALGO_TYPE_HPP +#define BOOST_INTRUSIVE_DETAIL_ALGO_TYPE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { + +enum algo_types +{ + CircularListAlgorithms, + CircularSListAlgorithms, + LinearSListAlgorithms, + CommonSListAlgorithms, + BsTreeAlgorithms, + RbTreeAlgorithms, + AvlTreeAlgorithms, + SgTreeAlgorithms, + SplayTreeAlgorithms, + TreapAlgorithms +}; + +template +struct get_algo; + +template +struct get_node_checker; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_ALGO_TYPE_HPP diff --git a/boost/intrusive/detail/any_node_and_algorithms.hpp b/boost/intrusive/detail/any_node_and_algorithms.hpp new file mode 100644 index 0000000..5a0e8c2 --- /dev/null +++ b/boost/intrusive/detail/any_node_and_algorithms.hpp @@ -0,0 +1,280 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_ANY_NODE_HPP +#define BOOST_INTRUSIVE_ANY_NODE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { + +template +struct any_node +{ + typedef any_node node; + typedef typename pointer_rebind::type node_ptr; + typedef typename pointer_rebind::type const_node_ptr; + node_ptr node_ptr_1; + node_ptr node_ptr_2; + node_ptr node_ptr_3; + std::size_t size_t_1; +}; + +template +struct any_list_node_traits +{ + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + static const node_ptr &get_next(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_next(const node_ptr & n, const node_ptr & next) + { n->node_ptr_1 = next; } + + static const node_ptr &get_previous(const const_node_ptr & n) + { return n->node_ptr_2; } + + static void set_previous(const node_ptr & n, const node_ptr & prev) + { n->node_ptr_2 = prev; } +}; + + +template +struct any_slist_node_traits +{ + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + static const node_ptr &get_next(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_next(const node_ptr & n, const node_ptr & next) + { n->node_ptr_1 = next; } +}; + + +template +struct any_unordered_node_traits + : public any_slist_node_traits +{ + typedef any_slist_node_traits reduced_slist_node_traits; + typedef typename reduced_slist_node_traits::node node; + typedef typename reduced_slist_node_traits::node_ptr node_ptr; + typedef typename reduced_slist_node_traits::const_node_ptr const_node_ptr; + + static const bool store_hash = true; + static const bool optimize_multikey = true; + + static const node_ptr &get_next(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_next(const node_ptr & n, const node_ptr & next) + { n->node_ptr_1 = next; } + + static node_ptr get_prev_in_group(const const_node_ptr & n) + { return n->node_ptr_2; } + + static void set_prev_in_group(const node_ptr & n, const node_ptr & prev) + { n->node_ptr_2 = prev; } + + static std::size_t get_hash(const const_node_ptr & n) + { return n->size_t_1; } + + static void set_hash(const node_ptr & n, std::size_t h) + { n->size_t_1 = h; } +}; + + +template +struct any_rbtree_node_traits +{ + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + typedef std::size_t color; + + static const node_ptr &get_parent(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_parent(const node_ptr & n, const node_ptr & p) + { n->node_ptr_1 = p; } + + static const node_ptr &get_left(const const_node_ptr & n) + { return n->node_ptr_2; } + + static void set_left(const node_ptr & n, const node_ptr & l) + { n->node_ptr_2 = l; } + + static const node_ptr &get_right(const const_node_ptr & n) + { return n->node_ptr_3; } + + static void set_right(const node_ptr & n, const node_ptr & r) + { n->node_ptr_3 = r; } + + static color get_color(const const_node_ptr & n) + { return n->size_t_1; } + + static void set_color(const node_ptr & n, color c) + { n->size_t_1 = c; } + + static color black() + { return 0u; } + + static color red() + { return 1u; } +}; + + +template +struct any_avltree_node_traits +{ + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + typedef std::size_t balance; + + static const node_ptr &get_parent(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_parent(const node_ptr & n, const node_ptr & p) + { n->node_ptr_1 = p; } + + static const node_ptr &get_left(const const_node_ptr & n) + { return n->node_ptr_2; } + + static void set_left(const node_ptr & n, const node_ptr & l) + { n->node_ptr_2 = l; } + + static const node_ptr &get_right(const const_node_ptr & n) + { return n->node_ptr_3; } + + static void set_right(const node_ptr & n, const node_ptr & r) + { n->node_ptr_3 = r; } + + static balance get_balance(const const_node_ptr & n) + { return n->size_t_1; } + + static void set_balance(const node_ptr & n, balance b) + { n->size_t_1 = b; } + + static balance negative() + { return 0u; } + + static balance zero() + { return 1u; } + + static balance positive() + { return 2u; } +}; + + +template +struct any_tree_node_traits +{ + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + static const node_ptr &get_parent(const const_node_ptr & n) + { return n->node_ptr_1; } + + static void set_parent(const node_ptr & n, const node_ptr & p) + { n->node_ptr_1 = p; } + + static const node_ptr &get_left(const const_node_ptr & n) + { return n->node_ptr_2; } + + static void set_left(const node_ptr & n, const node_ptr & l) + { n->node_ptr_2 = l; } + + static const node_ptr &get_right(const const_node_ptr & n) + { return n->node_ptr_3; } + + static void set_right(const node_ptr & n, const node_ptr & r) + { n->node_ptr_3 = r; } +}; + +template +class any_node_traits +{ + public: + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; +}; + +template +class any_algorithms +{ + template + static void function_not_available_for_any_hooks(typename detail::enable_if >::type) + {} + + public: + typedef any_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + typedef any_node_traits node_traits; + + //! Requires: node must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(const node_ptr & node) + { node->node_ptr_1 = 0; }; + + //! Effects: Returns true if node is in the same state as if called init(node) + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool inited(const const_node_ptr & node) + { return !node->node_ptr_1; }; + + static bool unique(const const_node_ptr & node) + { return 0 == node->node_ptr_1; } + + static void unlink(const node_ptr &) + { + //Auto-unlink hooks and unlink() are not available for any hooks + any_algorithms::template function_not_available_for_any_hooks(); + } + + static void swap_nodes(const node_ptr &, const node_ptr &) + { + //Any nodes have no swap_nodes capability because they don't know + //what algorithm they must use to unlink the node from the container + any_algorithms::template function_not_available_for_any_hooks(); + } +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_ANY_NODE_HPP diff --git a/boost/intrusive/detail/array_initializer.hpp b/boost/intrusive/detail/array_initializer.hpp new file mode 100644 index 0000000..b2072a8 --- /dev/null +++ b/boost/intrusive/detail/array_initializer.hpp @@ -0,0 +1,90 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP +#define BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace intrusive { +namespace detail { + +//This is not standard, but should work with all compilers +union max_align +{ + char char_; + short short_; + int int_; + long long_; + #ifdef BOOST_HAS_LONG_LONG + long long long_long_; + #endif + float float_; + double double_; + long double long_double_; + void * void_ptr_; +}; + +template +class array_initializer +{ + public: + template + array_initializer(const CommonInitializer &init) + { + char *init_buf = (char*)rawbuf; + std::size_t i = 0; + BOOST_TRY{ + for(; i != N; ++i){ + new(init_buf)T(init); + init_buf += sizeof(T); + } + } + BOOST_CATCH(...){ + while(i--){ + init_buf -= sizeof(T); + ((T*)init_buf)->~T(); + } + BOOST_RETHROW; + } + BOOST_CATCH_END + } + + operator T* () + { return (T*)(rawbuf); } + + operator const T*() const + { return (const T*)(rawbuf); } + + ~array_initializer() + { + char *init_buf = (char*)rawbuf + N*sizeof(T); + for(std::size_t i = 0; i != N; ++i){ + init_buf -= sizeof(T); + ((T*)init_buf)->~T(); + } + } + + private: + detail::max_align rawbuf[(N*sizeof(T)-1)/sizeof(detail::max_align)+1]; +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_ARRAY_INITIALIZER_HPP diff --git a/boost/intrusive/detail/assert.hpp b/boost/intrusive/detail/assert.hpp new file mode 100644 index 0000000..d75d225 --- /dev/null +++ b/boost/intrusive/detail/assert.hpp @@ -0,0 +1,41 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_ASSERT_HPP +#define BOOST_INTRUSIVE_DETAIL_ASSERT_HPP + +#if defined(_MSC_VER) +#pragma once +#endif + +#if !defined(BOOST_INTRUSIVE_INVARIANT_ASSERT) + #include + #define BOOST_INTRUSIVE_INVARIANT_ASSERT BOOST_ASSERT +#elif defined(BOOST_INTRUSIVE_INVARIANT_ASSERT_INCLUDE) + #include BOOST_INTRUSIVE_INVARIANT_ASSERT_INCLUDE +#endif + +#if !defined(BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT) + #include + #define BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT BOOST_ASSERT +#elif defined(BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT_INCLUDE) + #include BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT_INCLUDE +#endif + +#if !defined(BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT) + #include + #define BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT BOOST_ASSERT +#elif defined(BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT_INCLUDE) + #include BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT_INCLUDE +#endif + +#endif //BOOST_INTRUSIVE_DETAIL_ASSERT_HPP diff --git a/boost/intrusive/detail/avltree_node.hpp b/boost/intrusive/detail/avltree_node.hpp new file mode 100644 index 0000000..522806a --- /dev/null +++ b/boost/intrusive/detail/avltree_node.hpp @@ -0,0 +1,188 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_AVLTREE_NODE_HPP +#define BOOST_INTRUSIVE_AVLTREE_NODE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +///////////////////////////////////////////////////////////////////////////// +// // +// Generic node_traits for any pointer type // +// // +///////////////////////////////////////////////////////////////////////////// + +//This is the compact representation: 3 pointers +template +struct compact_avltree_node +{ + typedef typename pointer_rebind >::type node_ptr; + typedef typename pointer_rebind >::type const_node_ptr; + enum balance { negative_t, zero_t, positive_t }; + node_ptr parent_, left_, right_; +}; + +//This is the normal representation: 3 pointers + enum +template +struct avltree_node +{ + typedef typename pointer_rebind >::type node_ptr; + typedef typename pointer_rebind >::type const_node_ptr; + enum balance { negative_t, zero_t, positive_t }; + node_ptr parent_, left_, right_; + balance balance_; +}; + +//This is the default node traits implementation +//using a node with 3 generic pointers plus an enum +template +struct default_avltree_node_traits_impl +{ + typedef avltree_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + + typedef typename node::balance balance; + + static node_ptr get_parent(const const_node_ptr & n) + { return n->parent_; } + + static node_ptr get_parent(const node_ptr & n) + { return n->parent_; } + + static void set_parent(const node_ptr & n, const node_ptr & p) + { n->parent_ = p; } + + static node_ptr get_left(const const_node_ptr & n) + { return n->left_; } + + static node_ptr get_left(const node_ptr & n) + { return n->left_; } + + static void set_left(const node_ptr & n, const node_ptr & l) + { n->left_ = l; } + + static node_ptr get_right(const const_node_ptr & n) + { return n->right_; } + + static node_ptr get_right(const node_ptr & n) + { return n->right_; } + + static void set_right(const node_ptr & n, const node_ptr & r) + { n->right_ = r; } + + static balance get_balance(const const_node_ptr & n) + { return n->balance_; } + + static balance get_balance(const node_ptr & n) + { return n->balance_; } + + static void set_balance(const node_ptr & n, balance b) + { n->balance_ = b; } + + static balance negative() + { return node::negative_t; } + + static balance zero() + { return node::zero_t; } + + static balance positive() + { return node::positive_t; } +}; + +//This is the compact node traits implementation +//using a node with 3 generic pointers +template +struct compact_avltree_node_traits_impl +{ + typedef compact_avltree_node node; + typedef typename node::node_ptr node_ptr; + typedef typename node::const_node_ptr const_node_ptr; + typedef typename node::balance balance; + + typedef pointer_plus_bits ptr_bit; + + static node_ptr get_parent(const const_node_ptr & n) + { return ptr_bit::get_pointer(n->parent_); } + + static void set_parent(const node_ptr & n, const node_ptr & p) + { ptr_bit::set_pointer(n->parent_, p); } + + static node_ptr get_left(const const_node_ptr & n) + { return n->left_; } + + static void set_left(const node_ptr & n, const node_ptr & l) + { n->left_ = l; } + + static node_ptr get_right(const const_node_ptr & n) + { return n->right_; } + + static void set_right(const node_ptr & n, const node_ptr & r) + { n->right_ = r; } + + static balance get_balance(const const_node_ptr & n) + { return (balance)ptr_bit::get_bits(n->parent_); } + + static void set_balance(const node_ptr & n, balance b) + { ptr_bit::set_bits(n->parent_, (std::size_t)b); } + + static balance negative() + { return node::negative_t; } + + static balance zero() + { return node::zero_t; } + + static balance positive() + { return node::positive_t; } +}; + +//Dispatches the implementation based on the boolean +template +struct avltree_node_traits_dispatch + : public default_avltree_node_traits_impl +{}; + +template +struct avltree_node_traits_dispatch + : public compact_avltree_node_traits_impl +{}; + +//Inherit from rbtree_node_traits_dispatch depending on the embedding capabilities +template +struct avltree_node_traits + : public avltree_node_traits_dispatch + < VoidPointer + , OptimizeSize && + max_pointer_plus_bits + < VoidPointer + , detail::alignment_of >::value + >::value >= 2u + > +{}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_AVLTREE_NODE_HPP diff --git a/boost/intrusive/detail/common_slist_algorithms.hpp b/boost/intrusive/detail/common_slist_algorithms.hpp new file mode 100644 index 0000000..4c7f1a1 --- /dev/null +++ b/boost/intrusive/detail/common_slist_algorithms.hpp @@ -0,0 +1,194 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +class common_slist_algorithms +{ + public: + typedef typename NodeTraits::node node; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef NodeTraits node_traits; + + static node_ptr get_previous_node(node_ptr p, const node_ptr & this_node) + { + for( node_ptr p_next + ; this_node != (p_next = NodeTraits::get_next(p)) + ; p = p_next){ + //Logic error: possible use of linear lists with + //operations only permitted with lists + BOOST_INTRUSIVE_INVARIANT_ASSERT(p); + } + return p; + } + + static void init(const node_ptr & this_node) + { NodeTraits::set_next(this_node, node_ptr()); } + + static bool unique(const const_node_ptr & this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } + + static bool inited(const const_node_ptr & this_node) + { return !NodeTraits::get_next(this_node); } + + static void unlink_after(const node_ptr & prev_node) + { + const_node_ptr this_node(NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); + } + + static void unlink_after(const node_ptr & prev_node, const node_ptr & last_node) + { NodeTraits::set_next(prev_node, last_node); } + + static void link_after(const node_ptr & prev_node, const node_ptr & this_node) + { + NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, this_node); + } + + static void incorporate_after(const node_ptr & bp, const node_ptr & b, const node_ptr & be) + { + node_ptr p(NodeTraits::get_next(bp)); + NodeTraits::set_next(bp, b); + NodeTraits::set_next(be, p); + } + + static void transfer_after(const node_ptr & bp, const node_ptr & bb, const node_ptr & be) + { + if (bp != bb && bp != be && bb != be) { + node_ptr next_b = NodeTraits::get_next(bb); + node_ptr next_e = NodeTraits::get_next(be); + node_ptr next_p = NodeTraits::get_next(bp); + NodeTraits::set_next(bb, next_e); + NodeTraits::set_next(be, next_p); + NodeTraits::set_next(bp, next_b); + } + } + + struct stable_partition_info + { + std::size_t num_1st_partition; + std::size_t num_2nd_partition; + node_ptr beg_2st_partition; + node_ptr new_last_node; + }; + + template + static void stable_partition(node_ptr before_beg, const node_ptr &end, Pred pred, stable_partition_info &info) + { + node_ptr bcur = before_beg; + node_ptr cur = node_traits::get_next(bcur); + node_ptr new_f = end; + + std::size_t num1 = 0, num2 = 0; + while(cur != end){ + if(pred(cur)){ + ++num1; + bcur = cur; + cur = node_traits::get_next(cur); + } + else{ + ++num2; + node_ptr last_to_remove = bcur; + new_f = cur; + bcur = cur; + cur = node_traits::get_next(cur); + BOOST_TRY{ + //Main loop + while(cur != end){ + if(pred(cur)){ //Might throw + ++num1; + //Process current node + node_traits::set_next(last_to_remove, cur); + last_to_remove = cur; + node_ptr nxt = node_traits::get_next(cur); + node_traits::set_next(bcur, nxt); + cur = nxt; + } + else{ + ++num2; + bcur = cur; + cur = node_traits::get_next(cur); + } + } + } + BOOST_CATCH(...){ + node_traits::set_next(last_to_remove, new_f); + throw; + } + BOOST_CATCH_END + node_traits::set_next(last_to_remove, new_f); + break; + } + } + info.num_1st_partition = num1; + info.num_2nd_partition = num2; + info.beg_2st_partition = new_f; + info.new_last_node = bcur; + } + + //! Requires: f and l must be in a circular list. + //! + //! Effects: Returns the number of nodes in the range [f, l). + //! + //! Complexity: Linear + //! + //! Throws: Nothing. + static std::size_t distance(const const_node_ptr &f, const const_node_ptr &l) + { + const_node_ptr i(f); + std::size_t result = 0; + while(i != l){ + i = NodeTraits::get_next(i); + ++result; + } + return result; + } +}; + +/// @endcond + +} //namespace detail + +/// @cond + +template +struct get_algo +{ + typedef detail::common_slist_algorithms type; +}; + + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP diff --git a/boost/intrusive/detail/config_begin.hpp b/boost/intrusive/detail/config_begin.hpp new file mode 100644 index 0000000..36d605d --- /dev/null +++ b/boost/intrusive/detail/config_begin.hpp @@ -0,0 +1,51 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_CONFIG_HPP +#include +#endif + +#ifdef BOOST_MSVC + + #pragma warning (push) + // + //'function' : resolved overload was found by argument-dependent lookup + //A function found by argument-dependent lookup (Koenig lookup) was eventually + //chosen by overload resolution. + // + //In Visual C++ .NET and earlier compilers, a different function would have + //been called. To pick the original function, use an explicitly qualified name. + // + + //warning C4275: non dll-interface class 'x' used as base for + //dll-interface class 'Y' + #pragma warning (disable : 4275) + //warning C4251: 'x' : class 'y' needs to have dll-interface to + //be used by clients of class 'z' + #pragma warning (disable : 4251) + #pragma warning (disable : 4675) + #pragma warning (disable : 4996) + #pragma warning (disable : 4503) + #pragma warning (disable : 4284) // odd return type for operator-> + #pragma warning (disable : 4244) // possible loss of data + #pragma warning (disable : 4521) ////Disable "multiple copy constructors specified" + #pragma warning (disable : 4522) + #pragma warning (disable : 4146) + #pragma warning (disable : 4267) //conversion from 'X' to 'Y', possible loss of data + #pragma warning (disable : 4127) //conditional expression is constant + #pragma warning (disable : 4706) //assignment within conditional expression + #pragma warning (disable : 4541) //'typeid' used on polymorphic type 'boost::exception' with /GR- + #pragma warning (disable : 4512) //'typeid' used on polymorphic type 'boost::exception' with /GR- +#endif + +//#define BOOST_INTRUSIVE_USE_ITERATOR_FACADE +//#define BOOST_INTRUSIVE_USE_ITERATOR_ENABLE_IF_CONVERTIBLE diff --git a/boost/intrusive/detail/config_end.hpp b/boost/intrusive/detail/config_end.hpp new file mode 100644 index 0000000..a081443 --- /dev/null +++ b/boost/intrusive/detail/config_end.hpp @@ -0,0 +1,15 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#if defined BOOST_MSVC + #pragma warning (pop) +#endif diff --git a/boost/intrusive/detail/default_header_holder.hpp b/boost/intrusive/detail/default_header_holder.hpp new file mode 100644 index 0000000..c471691 --- /dev/null +++ b/boost/intrusive/detail/default_header_holder.hpp @@ -0,0 +1,65 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_DEFAULT_HEADER_HOLDER_HPP +#define BOOST_INTRUSIVE_DETAIL_DEFAULT_HEADER_HOLDER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +// trivial header node holder +template < typename NodeTraits > +struct default_header_holder : public NodeTraits::node +{ + typedef NodeTraits node_traits; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + + default_header_holder() : node() {} + + const_node_ptr get_node() const + { return pointer_traits< const_node_ptr >::pointer_to(*static_cast< const node* >(this)); } + + node_ptr get_node() + { return pointer_traits< node_ptr >::pointer_to(*static_cast< node* >(this)); } + + // (unsafe) downcast used to implement container-from-iterator + static default_header_holder* get_holder(const node_ptr &p) + { return static_cast< default_header_holder* >(boost::intrusive::detail::to_raw_pointer(p)); } +}; + +// type function producing the header node holder +template < typename Value_Traits, typename HeaderHolder > +struct get_header_holder_type +{ + typedef HeaderHolder type; +}; +template < typename Value_Traits > +struct get_header_holder_type< Value_Traits, void > +{ + typedef default_header_holder< typename Value_Traits::node_traits > type; +}; + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_DEFAULT_HEADER_HOLDER_HPP diff --git a/boost/intrusive/detail/ebo_functor_holder.hpp b/boost/intrusive/detail/ebo_functor_holder.hpp new file mode 100644 index 0000000..d2e8107 --- /dev/null +++ b/boost/intrusive/detail/ebo_functor_holder.hpp @@ -0,0 +1,226 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Joaquin M Lopez Munoz 2006-2013 +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP +#define BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace intrusive { +namespace detail { + +#if defined(BOOST_MSVC) || defined(__BORLANDC_) +#define BOOST_INTRUSIVE_TT_DECL __cdecl +#else +#define BOOST_INTRUSIVE_TT_DECL +#endif + +#if defined(_MSC_EXTENSIONS) && !defined(__BORLAND__) && !defined(_WIN64) && !defined(_M_ARM) && !defined(UNDER_CE) +#define BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = false; }; + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +// see boost ticket #4094 +// avoid duplicate definitions of is_unary_or_binary_function_impl +#ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#ifndef _MANAGED + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; + +template +struct is_unary_or_binary_function_impl +{ static const bool value = true; }; +#endif + +template +struct is_unary_or_binary_function_impl +{ static const bool value = false; }; + +template +struct is_unary_or_binary_function : is_unary_or_binary_function_impl +{}; + +template +class ebo_functor_holder_impl +{ + public: + ebo_functor_holder_impl() + {} + ebo_functor_holder_impl(const T& t) + : t_(t) + {} + template + ebo_functor_holder_impl(const Arg1& arg1, const Arg2& arg2) + : t_(arg1, arg2) + {} + + T& get(){return t_;} + const T& get()const{return t_;} + + private: + T t_; +}; + +template +class ebo_functor_holder_impl + : public T +{ + public: + ebo_functor_holder_impl() + {} + ebo_functor_holder_impl(const T& t) + : T(t) + {} + template + ebo_functor_holder_impl(const Arg1& arg1, const Arg2& arg2) + : T(arg1, arg2) + {} + + T& get(){return *this;} + const T& get()const{return *this;} +}; + +template +class ebo_functor_holder + : public ebo_functor_holder_impl::value> +{ + private: + typedef ebo_functor_holder_impl::value> super; + + public: + ebo_functor_holder(){} + ebo_functor_holder(const T& t) + : super(t) + {} + + template + ebo_functor_holder(const Arg1& arg1, const Arg2& arg2) + : super(arg1, arg2) + {} + + ebo_functor_holder& operator=(const ebo_functor_holder& x) + { + this->get()=x.get(); + return *this; + } +}; + + +} //namespace detail { +} //namespace intrusive { +} //namespace boost { + +#endif //#ifndef BOOST_INTRUSIVE_DETAIL_EBO_HOLDER_HPP diff --git a/boost/intrusive/detail/empty_node_checker.hpp b/boost/intrusive/detail/empty_node_checker.hpp new file mode 100644 index 0000000..33d78a3 --- /dev/null +++ b/boost/intrusive/detail/empty_node_checker.hpp @@ -0,0 +1,40 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_EMPTY_NODE_CHECKER_HPP +#define BOOST_INTRUSIVE_DETAIL_EMPTY_NODE_CHECKER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct empty_node_checker +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::const_node_ptr const_node_ptr; + + struct return_type {}; + + void operator () (const const_node_ptr&, const return_type&, const return_type&, return_type&) {} +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EMPTY_NODE_CHECKER_HPP diff --git a/boost/intrusive/detail/equal_to_value.hpp b/boost/intrusive/detail/equal_to_value.hpp new file mode 100644 index 0000000..3ffd1c2 --- /dev/null +++ b/boost/intrusive/detail/equal_to_value.hpp @@ -0,0 +1,44 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_EQUAL_TO_VALUE_HPP +#define BOOST_INTRUSIVE_DETAIL_EQUAL_TO_VALUE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +//This functor compares a stored value +//and the one passed as an argument +template +class equal_to_value +{ + ConstReference t_; + + public: + equal_to_value(ConstReference t) + : t_(t) + {} + + bool operator()(ConstReference t)const + { return t_ == t; } +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EQUAL_TO_VALUE_HPP diff --git a/boost/intrusive/detail/exception_disposer.hpp b/boost/intrusive/detail/exception_disposer.hpp new file mode 100644 index 0000000..1226f5e --- /dev/null +++ b/boost/intrusive/detail/exception_disposer.hpp @@ -0,0 +1,84 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_EXCEPTION_DISPOSER_HPP +#define BOOST_INTRUSIVE_DETAIL_EXCEPTION_DISPOSER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +template +class exception_disposer +{ + Container *cont_; + Disposer &disp_; + + exception_disposer(const exception_disposer&); + exception_disposer &operator=(const exception_disposer&); + + public: + exception_disposer(Container &cont, Disposer &disp) + : cont_(&cont), disp_(disp) + {} + + void release() + { cont_ = 0; } + + ~exception_disposer() + { + if(cont_){ + cont_->clear_and_dispose(disp_); + } + } +}; + +template +class exception_array_disposer +{ + Container *cont_; + Disposer &disp_; + SizeType &constructed_; + + exception_array_disposer(const exception_array_disposer&); + exception_array_disposer &operator=(const exception_array_disposer&); + + public: + + exception_array_disposer + (Container &cont, Disposer &disp, SizeType &constructed) + : cont_(&cont), disp_(disp), constructed_(constructed) + {} + + void release() + { cont_ = 0; } + + ~exception_array_disposer() + { + SizeType n = constructed_; + if(cont_){ + while(n--){ + cont_[n].clear_and_dispose(disp_); + } + } + } +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_EXCEPTION_DISPOSER_HPP diff --git a/boost/intrusive/detail/function_detector.hpp b/boost/intrusive/detail/function_detector.hpp new file mode 100644 index 0000000..f72865d --- /dev/null +++ b/boost/intrusive/detail/function_detector.hpp @@ -0,0 +1,88 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2009-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +// This code was modified from the code posted by Alexandre Courpron in his +// article "Interface Detection" in The Code Project: +// http://www.codeproject.com/KB/architecture/Detector.aspx +/////////////////////////////////////////////////////////////////////////////// +// Copyright 2007 Alexandre Courpron +// +// Permission to use, copy, modify, redistribute and sell this software, +// provided that this copyright notice appears on all copies of the software. +/////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP +#define BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { +namespace function_detector { + + typedef char NotFoundType; + struct StaticFunctionType { NotFoundType x [2]; }; + struct NonStaticFunctionType { NotFoundType x [3]; }; + + enum + { NotFound = 0, + StaticFunction = sizeof( StaticFunctionType ) - sizeof( NotFoundType ), + NonStaticFunction = sizeof( NonStaticFunctionType ) - sizeof( NotFoundType ) + }; + +} //namespace boost { +} //namespace intrusive { +} //namespace function_detector { + +#define BOOST_INTRUSIVE_CREATE_FUNCTION_DETECTOR(Identifier, InstantiationKey) \ + namespace boost { \ + namespace intrusive { \ + namespace function_detector { \ + template < class T, \ + class NonStaticType, \ + class NonStaticConstType, \ + class StaticType > \ + class DetectMember_##InstantiationKey_##Identifier { \ + template < NonStaticType > \ + struct TestNonStaticNonConst ; \ + \ + template < NonStaticConstType > \ + struct TestNonStaticConst ; \ + \ + template < StaticType > \ + struct TestStatic ; \ + \ + template \ + static NonStaticFunctionType Test( TestNonStaticNonConst<&U::Identifier>*, int ); \ + \ + template \ + static NonStaticFunctionType Test( TestNonStaticConst<&U::Identifier>*, int ); \ + \ + template \ + static StaticFunctionType Test( TestStatic<&U::Identifier>*, int ); \ + \ + template \ + static NotFoundType Test( ... ); \ + public : \ + static const int check = NotFound + (sizeof(Test(0, 0)) - sizeof(NotFoundType));\ + };\ +}}} //namespace boost::intrusive::function_detector { + +#define BOOST_INTRUSIVE_DETECT_FUNCTION(Class, InstantiationKey, ReturnType, Identifier, Params) \ + ::boost::intrusive::function_detector::DetectMember_##InstantiationKey_##Identifier< Class,\ + ReturnType (Class::*)Params,\ + ReturnType (Class::*)Params const,\ + ReturnType (*)Params \ + >::check + +#endif //@ifndef BOOST_INTRUSIVE_DETAIL_FUNCTION_DETECTOR_HPP diff --git a/boost/intrusive/detail/generic_hook.hpp b/boost/intrusive/detail/generic_hook.hpp new file mode 100644 index 0000000..c5af081 --- /dev/null +++ b/boost/intrusive/detail/generic_hook.hpp @@ -0,0 +1,217 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP +#define BOOST_INTRUSIVE_GENERIC_HOOK_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond + +namespace detail { + +template +struct link_dispatch +{}; + +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ //If this assertion raises, you might have destroyed an object + //while it was still inserted in a container that is alive. + //If so, remove the object from the container before destroying it. + (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); +} + +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ hook.unlink(); } + +template +void destructor_impl(Hook &, detail::link_dispatch) +{} + +} //namespace detail { + +enum base_hook_type +{ NoBaseHookId +, ListBaseHookId +, SlistBaseHookId +, RbTreeBaseHookId +, HashBaseHookId +, AvlTreeBaseHookId +, BsTreeBaseHookId +, TreapTreeBaseHookId +, AnyBaseHookId +}; + + +template +struct hook_tags_definer{}; + +template +struct hook_tags_definer +{ typedef HookTags default_list_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_slist_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_rbtree_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_hashtable_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_avltree_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_bstree_hook; }; + +template +struct hook_tags_definer +{ typedef HookTags default_any_hook; }; + +template + < class NodeTraits + , class Tag + , link_mode_type LinkMode + , base_hook_type BaseHookType + > +struct hooktags_impl +{ + static const link_mode_type link_mode = LinkMode; + typedef Tag tag; + typedef NodeTraits node_traits; + static const bool is_base_hook = !detail::is_same::value; + static const bool safemode_or_autounlink = is_safe_autounlink::value; + static const unsigned int type = BaseHookType; +}; + +/// @endcond + +template + < class NodeAlgorithms + , class Tag + , link_mode_type LinkMode + , base_hook_type BaseHookType + > +class generic_hook + /// @cond + //If the hook is a base hook, derive generic hook from node_holder + //so that a unique base class is created to convert from the node + //to the type. This mechanism will be used by bhtraits. + // + //If the hook is a member hook, generic hook will directly derive + //from the hook. + : public detail::if_c + < detail::is_same::value + , typename NodeAlgorithms::node + , node_holder + >::type + //If this is the a default-tagged base hook derive from a class that + //will define an special internal typedef. Containers will be able to detect this + //special typedef and obtain generic_hook's internal types in order to deduce + //value_traits for this hook. + , public hook_tags_definer + < generic_hook + , detail::is_same::value*BaseHookType> + /// @endcond +{ + /// @cond + typedef NodeAlgorithms node_algorithms; + typedef typename node_algorithms::node node; + typedef typename node_algorithms::node_ptr node_ptr; + typedef typename node_algorithms::const_node_ptr const_node_ptr; + + public: + + typedef hooktags_impl + < typename NodeAlgorithms::node_traits + , Tag, LinkMode, BaseHookType> hooktags; + + node_ptr this_ptr() + { return pointer_traits::pointer_to(static_cast(*this)); } + + const_node_ptr this_ptr() const + { return pointer_traits::pointer_to(static_cast(*this)); } + + public: + /// @endcond + + generic_hook() + { + if(hooktags::safemode_or_autounlink){ + node_algorithms::init(this->this_ptr()); + } + } + + generic_hook(const generic_hook& ) + { + if(hooktags::safemode_or_autounlink){ + node_algorithms::init(this->this_ptr()); + } + } + + generic_hook& operator=(const generic_hook& ) + { return *this; } + + ~generic_hook() + { + destructor_impl + (*this, detail::link_dispatch()); + } + + void swap_nodes(generic_hook &other) + { + node_algorithms::swap_nodes + (this->this_ptr(), other.this_ptr()); + } + + bool is_linked() const + { + //is_linked() can be only used in safe-mode or auto-unlink + BOOST_STATIC_ASSERT(( hooktags::safemode_or_autounlink )); + return !node_algorithms::unique(this->this_ptr()); + } + + void unlink() + { + BOOST_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink )); + node_ptr n(this->this_ptr()); + if(!node_algorithms::inited(n)){ + node_algorithms::unlink(n); + node_algorithms::init(n); + } + } +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP diff --git a/boost/intrusive/detail/get_value_traits.hpp b/boost/intrusive/detail/get_value_traits.hpp new file mode 100644 index 0000000..6f5a9e3 --- /dev/null +++ b/boost/intrusive/detail/get_value_traits.hpp @@ -0,0 +1,218 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_GET_VALUE_TRAITS_HPP +#define BOOST_INTRUSIVE_DETAIL_GET_VALUE_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { + +#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED + +template +struct is_default_hook_tag +{ static const bool value = false; }; + +namespace detail{ + +template +struct concrete_hook_base_value_traits +{ + typedef typename BaseHook::hooktags tags; + typedef bhtraits + < T + , typename tags::node_traits + , tags::link_mode + , typename tags::tag + , tags::type> type; +}; + +template +struct concrete_hook_base_value_traits +{ + typedef typename BaseHook::hooktags type; +}; + +template +struct any_hook_base_value_traits +{ + //AnyToSomeHook value_traits derive from a generic_hook + //The generic_hook is configured with any_node_traits + //and AnyToSomeHook::value_traits with the correct + //node traits for the container, so use node_traits + //from AnyToSomeHook_ProtoValueTraits and the rest of + //elements from the hooktags member of the generic_hook + + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + < typename basic_hook_t::hooktags::node_traits::node_ptr + , void>::type void_pointer; + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type node_traits; + + typedef bhtraits + < T + , node_traits + , basic_hook_t::hooktags::link_mode + , typename basic_hook_t::hooktags::tag + , basic_hook_t::hooktags::type + > type; +}; + +template +struct any_hook_base_value_traits +{ + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + < typename basic_hook_t::hooktags::node_traits::node_ptr + , void>::type void_pointer; + + struct type + { + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type node_traits; + }; +}; + +template +struct get_member_value_traits +{ + typedef typename MemberHook::member_value_traits type; +}; + +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(internal_any_hook, is_any_hook) +BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(internal_base_hook, hooktags::is_base_hook) + +template +struct internal_member_value_traits +{ + template static one test(...); + template static two test(typename U::member_value_traits* = 0); + static const bool value = sizeof(test(0)) == sizeof(two); +}; + +template::value> +struct supposed_value_traits; + +template::value> +struct get_base_value_traits; + +template::value> +struct supposed_base_value_traits; + +template::value> +struct supposed_member_value_traits; + +template::value> +struct any_or_concrete_value_traits; + +//Base any hook +template +struct get_base_value_traits + : any_hook_base_value_traits +{}; + +//Non-any base hook +template +struct get_base_value_traits + : concrete_hook_base_value_traits +{}; + +//...It's a default hook +template +struct supposed_value_traits +{ typedef typename SupposedValueTraits::template apply::type type; }; + +//...Not a default hook +template +struct supposed_value_traits +{ typedef SupposedValueTraits type; }; + +//...It's a base hook +template +struct supposed_base_value_traits + : get_base_value_traits +{}; + +//...Not a base hook, try if it's a member or value_traits +template +struct supposed_base_value_traits + : supposed_member_value_traits +{}; + +//...It's a member hook +template +struct supposed_member_value_traits + : get_member_value_traits +{}; + +//...Not a member hook +template +struct supposed_member_value_traits + : any_or_concrete_value_traits +{}; + +template +struct any_or_concrete_value_traits +{ + //A hook node (non-base, e.g.: member or other value traits + typedef typename AnyToSomeHook_ProtoValueTraits::basic_hook_t basic_hook_t; + typedef typename pointer_rebind + ::type void_pointer; + typedef typename AnyToSomeHook_ProtoValueTraits::template + node_traits_from_voidptr::type any_node_traits; + + struct type : basic_hook_t + { + typedef any_node_traits node_traits; + }; +}; + +template +struct any_or_concrete_value_traits +{ + typedef SupposedValueTraits type; +}; + +//////////////////////////////////////// +// get_value_traits / get_node_traits +//////////////////////////////////////// + +template +struct get_value_traits + : supposed_base_value_traits::type, T> +{}; + +template +struct get_node_traits +{ + typedef typename get_value_traits::type::node_traits type; +}; + +} //namespace detail{ + +#endif //BOOST_INTRUSIVE_DOXYGEN_INVOKED + +} //namespace intrusive { +} //namespace boost { + +#include + +#endif //#ifndef BOOST_INTRUSIVE_DETAIL_GET_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/has_member_function_callable_with.hpp b/boost/intrusive/detail/has_member_function_callable_with.hpp new file mode 100644 index 0000000..ca96f15 --- /dev/null +++ b/boost/intrusive/detail/has_member_function_callable_with.hpp @@ -0,0 +1,372 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +// sample.h + +#if !defined(BOOST_PP_IS_ITERATING) + + #ifndef BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED + #define BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED + + #include + #include + #include + + + //Mark that we don't support 0 arg calls due to compiler ICE in GCC 3.4/4.0/4.1 and + //wrong SFINAE for GCC 4.2/4.3 + #if defined(__GNUC__) && !defined(__clang__) && ((__GNUC__*100 + __GNUC_MINOR__*10) >= 340) && ((__GNUC__*100 + __GNUC_MINOR__*10) <= 430) + #define BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED + #elif defined(BOOST_INTEL) && (BOOST_INTEL < 1200 ) + #define BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED + #endif + + namespace boost_intrusive_has_member_function_callable_with { + + struct dont_care + { + dont_care(...); + }; + + template + struct make_dontcare + { + typedef boost_intrusive_has_member_function_callable_with::dont_care type; + }; + + struct private_type + { + static private_type p; + private_type const &operator,(int) const; + }; + + typedef char yes_type; // sizeof(yes_type) == 1 + struct no_type{ char dummy[2]; }; // sizeof(no_type) == 2 + + template + no_type is_private_type(T const &); + yes_type is_private_type(private_type const &); + + } //boost_intrusive_has_member_function_callable_with + + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_SINGLE_ITERATION + #endif + + #endif //BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_DETAILS_INCLUDED + +#else //!BOOST_PP_IS_ITERATING + + #ifndef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + #error "BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME not defined!" + #endif + + #ifndef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + #error "BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN not defined!" + #endif + + #ifndef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + #error "BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END not defined!" + #endif + + #if BOOST_PP_ITERATION_START() > BOOST_PP_ITERATION_FINISH() + #error "BOOST_PP_ITERATION_START() must be <= BOOST_PP_ITERATION_FINISH()" + #endif + + #if BOOST_PP_ITERATION() == BOOST_PP_ITERATION_START() + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + + template + class BOOST_PP_CAT(has_member_function_named_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + struct BaseMixin + { + void BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME(); + }; + + struct Base : public ::boost::intrusive::detail::remove_cv::type, public BaseMixin { Base(); }; + template class Helper{}; + + template + static boost_intrusive_has_member_function_callable_with::no_type deduce + (U*, Helper* = 0); + static boost_intrusive_has_member_function_callable_with::yes_type deduce(...); + + public: + static const bool value = + sizeof(boost_intrusive_has_member_function_callable_with::yes_type) == sizeof(deduce((Base*)(0))); + }; + + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME), _impl); + //! + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME), _impl) + + { + static const bool value = false; + }; + //! + + #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl); + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + static const bool value = false; + }; + + #ifdef BOOST_NO_CXX11_DECLTYPE + + //Special case for 0 args + template< class F + , std::size_t N = + sizeof((boost::move_detail::declval(). + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME (), 0))> + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::yes_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + //For buggy compilers like MSVC 7.1+ ((F*)0)->func() does not + //SFINAE-out the zeroarg_checker_ instantiation but sizeof yields to 0. + template + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::no_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + #endif //#ifdef BOOST_NO_CXX11_DECLTYPE + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + #ifndef BOOST_NO_CXX11_DECLTYPE + template().BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME()) > + static boost_intrusive_has_member_function_callable_with::yes_type Test(U*); + #else + template + static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + Test(BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)*); + #endif + + template + static boost_intrusive_has_member_function_callable_with::no_type Test(...); + + static const bool value = sizeof(Test< Fun >(0)) + == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); + }; + + template + struct BOOST_PP_CAT( BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME), _impl) + + { + + template + struct FunWrapTmpl : Fun + { + FunWrapTmpl(); + using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; + + boost_intrusive_has_member_function_callable_with::private_type + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( DontCares...) const; + }; + + typedef FunWrapTmpl::type...> FunWrap; + + static bool const value = (sizeof(boost_intrusive_has_member_function_callable_with::no_type) == + sizeof(boost_intrusive_has_member_function_callable_with::is_private_type + ( (::boost::move_detail::declval< FunWrap >(). + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( ::boost::move_detail::declval()... ), 0) ) + ) + ); + }; + + template + struct BOOST_PP_CAT( has_member_function_callable_with_ + , BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + : public BOOST_PP_CAT( BOOST_PP_CAT(has_member_function_callable_with_ + , BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + < Fun + , BOOST_PP_CAT( has_member_function_named_ + , BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME )::value + , Args... > + {}; + + #endif //defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + + #endif //BOOST_PP_ITERATION() == BOOST_PP_ITERATION_START() + + #if BOOST_PP_ITERATION() == 0 + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #if !defined(_MSC_VER) || (_MSC_VER < 1600) + + #if defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + //Mark that we don't support 0 arg calls due to compiler ICE in GCC 3.4/4.0/4.1 and + //wrong SFINAE for GCC 4.2/4.3 + static const bool value = true; + }; + + #else //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + //Special case for 0 args + template< class F + , std::size_t N = + sizeof((boost::move_detail::declval(). + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME (), 0))> + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::yes_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + //For buggy compilers like MSVC 7.1+ ((F*)0)->func() does not + //SFINAE-out the zeroarg_checker_ instantiation but sizeof yields to 0. + template + struct BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + { + boost_intrusive_has_member_function_callable_with::no_type dummy; + BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)(int); + }; + + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + template + static BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + Test(BOOST_PP_CAT(zeroarg_checker_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME)*); + + template + static boost_intrusive_has_member_function_callable_with::no_type Test(...); + + static const bool value = sizeof(Test< Fun >(0)) + == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); + }; + #endif //defined(BOOST_INTRUSIVE_DETAIL_HAS_MEMBER_FUNCTION_CALLABLE_WITH_0_ARGS_UNSUPPORTED) + + #else //#if !defined(_MSC_VER) || (_MSC_VER < 1600) + template + struct BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + template + static decltype( boost::move_detail::declval().BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME() + , boost_intrusive_has_member_function_callable_with::yes_type()) + Test(U*); + + template + static boost_intrusive_has_member_function_callable_with::no_type Test(...); + + static const bool value = sizeof(Test(0)) + == sizeof(boost_intrusive_has_member_function_callable_with::yes_type); + }; + #endif //#if !defined(_MSC_VER) || (_MSC_VER < 1600) + + #else //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + + #else //BOOST_PP_ITERATION() == 0 + + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + + template + struct BOOST_PP_CAT( BOOST_PP_CAT(has_member_function_callable_with_ + , BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME),_impl) + + { + struct FunWrap : Fun + { + FunWrap(); + + using Fun::BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME; + boost_intrusive_has_member_function_callable_with::private_type + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( BOOST_PP_ENUM(BOOST_PP_ITERATION() + , BOOST_INTRUSIVE_PP_IDENTITY + , boost_intrusive_has_member_function_callable_with::dont_care)) const; + }; + + static bool const value = + (sizeof(boost_intrusive_has_member_function_callable_with::no_type) == + sizeof(boost_intrusive_has_member_function_callable_with::is_private_type + ( (boost::move_detail::declval(). + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + ( BOOST_PP_ENUM( BOOST_PP_ITERATION(), BOOST_INTRUSIVE_PP_DECLVAL, _) ), 0 + ) + ) + ) + ); + }; + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #endif //BOOST_PP_ITERATION() == 0 + + #if BOOST_PP_ITERATION() == BOOST_PP_ITERATION_FINISH() + + #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + + template + struct BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME) + : public BOOST_PP_CAT(BOOST_PP_CAT(has_member_function_callable_with_, BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME), _impl) + ::value + BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION_FINISH(), P) > + {}; + + BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + + #endif //#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + #undef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME + #undef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN + #undef BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END + + #endif //#if BOOST_PP_ITERATION() == BOOST_PP_ITERATION_FINISH() + +#endif //!BOOST_PP_IS_ITERATING diff --git a/boost/intrusive/detail/hashtable_node.hpp b/boost/intrusive/detail/hashtable_node.hpp new file mode 100644 index 0000000..6449fa0 --- /dev/null +++ b/boost/intrusive/detail/hashtable_node.hpp @@ -0,0 +1,287 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_HASHTABLE_NODE_HPP +#define BOOST_INTRUSIVE_HASHTABLE_NODE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include //make_slist +#include +#include +#include + + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct bucket_impl : public Slist +{ + typedef Slist slist_type; + bucket_impl() + {} + + bucket_impl(const bucket_impl &) + {} + + ~bucket_impl() + { + //This bucket is still being used! + BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty()); + } + + bucket_impl &operator=(const bucket_impl&) + { + //This bucket is still in use! + BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty()); + //Slist::clear(); + return *this; + } +}; + +template +struct bucket_traits_impl +{ + private: + BOOST_COPYABLE_AND_MOVABLE(bucket_traits_impl) + + public: + /// @cond + + typedef typename pointer_traits + ::template rebind_pointer + < bucket_impl >::type bucket_ptr; + typedef Slist slist; + typedef typename Slist::size_type size_type; + /// @endcond + + bucket_traits_impl(bucket_ptr buckets, size_type len) + : buckets_(buckets), buckets_len_(len) + {} + + bucket_traits_impl(const bucket_traits_impl &x) + : buckets_(x.buckets_), buckets_len_(x.buckets_len_) + {} + + bucket_traits_impl(BOOST_RV_REF(bucket_traits_impl) x) + : buckets_(x.buckets_), buckets_len_(x.buckets_len_) + { x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; } + + bucket_traits_impl& operator=(BOOST_RV_REF(bucket_traits_impl) x) + { + buckets_ = x.buckets_; buckets_len_ = x.buckets_len_; + x.buckets_ = bucket_ptr(); x.buckets_len_ = 0; return *this; + } + + bucket_traits_impl& operator=(BOOST_COPY_ASSIGN_REF(bucket_traits_impl) x) + { + buckets_ = x.buckets_; buckets_len_ = x.buckets_len_; return *this; + } + + const bucket_ptr &bucket_begin() const + { return buckets_; } + + size_type bucket_count() const + { return buckets_len_; } + + private: + bucket_ptr buckets_; + size_type buckets_len_; +}; + +template +struct hash_reduced_slist_node_traits +{ + template static detail::one test(...); + template static detail::two test(typename U::reduced_slist_node_traits* = 0); + static const bool value = sizeof(test(0)) == sizeof(detail::two); +}; + +template +struct apply_reduced_slist_node_traits +{ + typedef typename NodeTraits::reduced_slist_node_traits type; +}; + +template +struct reduced_slist_node_traits +{ + typedef typename detail::eval_if_c + < hash_reduced_slist_node_traits::value + , apply_reduced_slist_node_traits + , detail::identity + >::type type; +}; + +template +struct get_slist_impl +{ + typedef trivial_value_traits trivial_traits; + + //Reducing symbol length + struct type : make_slist + < typename NodeTraits::node + , boost::intrusive::value_traits + , boost::intrusive::constant_time_size + , boost::intrusive::size_type + >::type + {}; +}; + +} //namespace detail { + +template +class hashtable_iterator +{ + typedef boost::intrusive::iterator + < std::forward_iterator_tag + , typename BucketValueTraits::value_traits::value_type + , typename pointer_traits::difference_type + , typename detail::add_const_if_c + ::type * + , typename detail::add_const_if_c + ::type & + > iterator_traits; + + typedef typename BucketValueTraits::value_traits value_traits; + typedef typename BucketValueTraits::bucket_traits bucket_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename detail::get_slist_impl + ::type + >::type slist_impl; + typedef typename slist_impl::iterator siterator; + typedef typename slist_impl::const_iterator const_siterator; + typedef detail::bucket_impl bucket_type; + + typedef typename pointer_traits + ::template rebind_pointer + < const BucketValueTraits >::type const_bucketvaltraits_ptr; + typedef typename slist_impl::size_type size_type; + + + static typename node_traits::node_ptr downcast_bucket(typename bucket_type::node_ptr p) + { + return pointer_traits:: + pointer_to(static_cast(*p)); + } + + public: + typedef typename iterator_traits::difference_type difference_type; + typedef typename iterator_traits::value_type value_type; + typedef typename iterator_traits::pointer pointer; + typedef typename iterator_traits::reference reference; + typedef typename iterator_traits::iterator_category iterator_category; + + hashtable_iterator () + : slist_it_() //Value initialization to achieve "null iterators" (N3644) + {} + + explicit hashtable_iterator(siterator ptr, const BucketValueTraits *cont) + : slist_it_ (ptr), traitsptr_ (cont ? pointer_traits::pointer_to(*cont) : const_bucketvaltraits_ptr() ) + {} + + hashtable_iterator(const hashtable_iterator &other) + : slist_it_(other.slist_it()), traitsptr_(other.get_bucket_value_traits()) + {} + + const siterator &slist_it() const + { return slist_it_; } + + hashtable_iterator unconst() const + { return hashtable_iterator(this->slist_it(), this->get_bucket_value_traits()); } + + public: + hashtable_iterator& operator++() + { this->increment(); return *this; } + + hashtable_iterator operator++(int) + { + hashtable_iterator result (*this); + this->increment(); + return result; + } + + friend bool operator== (const hashtable_iterator& i, const hashtable_iterator& i2) + { return i.slist_it_ == i2.slist_it_; } + + friend bool operator!= (const hashtable_iterator& i, const hashtable_iterator& i2) + { return !(i == i2); } + + reference operator*() const + { return *this->operator ->(); } + + pointer operator->() const + { + return boost::intrusive::detail::to_raw_pointer(this->priv_value_traits().to_value_ptr + (downcast_bucket(slist_it_.pointed_node()))); + } + + const const_bucketvaltraits_ptr &get_bucket_value_traits() const + { return traitsptr_; } + + const value_traits &priv_value_traits() const + { return traitsptr_->priv_value_traits(); } + + const bucket_traits &priv_bucket_traits() const + { return traitsptr_->priv_bucket_traits(); } + + private: + void increment() + { + const bucket_traits &rbuck_traits = this->priv_bucket_traits(); + bucket_type* const buckets = boost::intrusive::detail::to_raw_pointer(rbuck_traits.bucket_begin()); + const size_type buckets_len = rbuck_traits.bucket_count(); + + ++slist_it_; + const typename slist_impl::node_ptr n = slist_it_.pointed_node(); + const siterator first_bucket_bbegin = buckets->end(); + if(first_bucket_bbegin.pointed_node() <= n && n <= buckets[buckets_len-1].cend().pointed_node()){ + //If one-past the node is inside the bucket then look for the next non-empty bucket + //1. get the bucket_impl from the iterator + const bucket_type &b = static_cast + (bucket_type::slist_type::container_from_end_iterator(slist_it_)); + + //2. Now just calculate the index b has in the bucket array + size_type n_bucket = static_cast(&b - buckets); + + //3. Iterate until a non-empty bucket is found + do{ + if (++n_bucket >= buckets_len){ //bucket overflow, return end() iterator + slist_it_ = buckets->before_begin(); + return; + } + } + while (buckets[n_bucket].empty()); + slist_it_ = buckets[n_bucket].begin(); + } + else{ + //++slist_it_ yield to a valid object + } + } + + siterator slist_it_; + const_bucketvaltraits_ptr traitsptr_; +}; + +} //namespace intrusive { +} //namespace boost { + +#endif diff --git a/boost/intrusive/detail/hook_traits.hpp b/boost/intrusive/detail/hook_traits.hpp new file mode 100644 index 0000000..c9c115b --- /dev/null +++ b/boost/intrusive/detail/hook_traits.hpp @@ -0,0 +1,182 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP +#define BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +template +struct bhtraits_base +{ + public: + typedef NodePtr node_ptr; + typedef typename pointer_traits::element_type node; + typedef node_holder node_holder_type; + typedef T value_type; + typedef typename pointer_traits:: + template rebind_pointer::type const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + //typedef typename pointer_traits::reference reference; + //typedef typename pointer_traits::reference const_reference; + typedef T & reference; + typedef const T & const_reference; + typedef node_holder_type & node_holder_reference; + typedef const node_holder_type & const_node_holder_reference; + typedef node& node_reference; + typedef const node & const_node_reference; + + static pointer to_value_ptr(const node_ptr & n) + { + return pointer_traits::pointer_to + (static_cast(static_cast(*n))); + } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { + return pointer_traits::pointer_to + (static_cast(static_cast(*n))); + } + + static node_ptr to_node_ptr(reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value))); + } + + static const_node_ptr to_node_ptr(const_reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value))); + } +}; + +template +struct bhtraits + : public bhtraits_base +{ + static const link_mode_type link_mode = LinkMode; + typedef NodeTraits node_traits; +}; + + +template +struct mhtraits +{ + public: + typedef Hook hook_type; + typedef typename hook_type::hooktags::node_traits node_traits; + typedef typename node_traits::node node; + typedef T value_type; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + typedef T & reference; + typedef const T & const_reference; + typedef node& node_reference; + typedef const node & const_node_reference; + typedef hook_type& hook_reference; + typedef const hook_type & const_hook_reference; + + static const link_mode_type link_mode = Hook::hooktags::link_mode; + + static node_ptr to_node_ptr(reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value.*P))); + } + + static const_node_ptr to_node_ptr(const_reference value) + { + return pointer_traits::pointer_to + (static_cast(static_cast(value.*P))); + } + + static pointer to_value_ptr(const node_ptr & n) + { + return pointer_traits::pointer_to + (*detail::parent_from_member + (static_cast(boost::intrusive::detail::to_raw_pointer(n)), P)); + } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { + return pointer_traits::pointer_to + (*detail::parent_from_member + (static_cast(boost::intrusive::detail::to_raw_pointer(n)), P)); + } +}; + + +template +struct fhtraits +{ + public: + typedef typename Functor::hook_type hook_type; + typedef typename Functor::hook_ptr hook_ptr; + typedef typename Functor::const_hook_ptr const_hook_ptr; + typedef typename hook_type::hooktags::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename Functor::value_type value_type; + typedef typename node_traits::node_ptr node_ptr; + typedef typename node_traits::const_node_ptr const_node_ptr; + typedef typename pointer_traits:: + template rebind_pointer::type pointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_pointer; + typedef value_type & reference; + typedef const value_type & const_reference; + static const link_mode_type link_mode = hook_type::hooktags::link_mode; + + static node_ptr to_node_ptr(reference value) + { return static_cast(boost::intrusive::detail::to_raw_pointer(Functor::to_hook_ptr(value))); } + + static const_node_ptr to_node_ptr(const_reference value) + { return static_cast(boost::intrusive::detail::to_raw_pointer(Functor::to_hook_ptr(value))); } + + static pointer to_value_ptr(const node_ptr & n) + { return Functor::to_value_ptr(to_hook_ptr(n)); } + + static const_pointer to_value_ptr(const const_node_ptr & n) + { return Functor::to_value_ptr(to_hook_ptr(n)); } + + private: + static hook_ptr to_hook_ptr(const node_ptr & n) + { return hook_ptr(&*static_cast(&*n)); } + + static const_hook_ptr to_hook_ptr(const const_node_ptr & n) + { return const_hook_ptr(&*static_cast(&*n)); } +}; + + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP diff --git a/boost/intrusive/detail/iiterator.hpp b/boost/intrusive/detail/iiterator.hpp new file mode 100644 index 0000000..8378ead --- /dev/null +++ b/boost/intrusive/detail/iiterator.hpp @@ -0,0 +1,227 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_IITERATOR_HPP +#define BOOST_INTRUSIVE_DETAIL_IITERATOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +#include + + +namespace boost { +namespace intrusive { + +template +struct iterator +{ + typedef Category iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef Pointer pointer; + typedef Reference reference; +}; + +template +struct iterator_traits +{ + typedef typename Iterator::difference_type difference_type; + typedef typename Iterator::value_type value_type; + typedef typename Iterator::pointer pointer; + typedef typename Iterator::reference reference; + typedef typename Iterator::iterator_category iterator_category; +}; + +template +struct iterator_traits +{ + typedef std::ptrdiff_t difference_type; + typedef T value_type; + typedef T* pointer; + typedef T& reference; + typedef std::random_access_iterator_tag iterator_category; +}; + +template +struct iterator_traits +{ + typedef std::ptrdiff_t difference_type; + typedef T value_type; + typedef const T* pointer; + typedef const T& reference; + typedef std::random_access_iterator_tag iterator_category; +}; + +template +struct value_traits_pointers +{ + typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT + (boost::intrusive::detail:: + , ValueTraits, value_traits_ptr + , typename pointer_traits::template + rebind_pointer::type) value_traits_ptr; + + typedef typename pointer_traits::template + rebind_pointer::type const_value_traits_ptr; +}; + +template +struct iiterator +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node node; + typedef typename node_traits::node_ptr node_ptr; + typedef ::boost::intrusive::pointer_traits nodepointer_traits_t; + typedef typename nodepointer_traits_t::template + rebind_pointer::type void_pointer; + typedef typename ValueTraits::value_type value_type; + typedef typename ValueTraits::pointer nonconst_pointer; + typedef typename ValueTraits::const_pointer yesconst_pointer; + typedef typename ::boost::intrusive::pointer_traits + ::reference nonconst_reference; + typedef typename ::boost::intrusive::pointer_traits + ::reference yesconst_reference; + typedef typename nodepointer_traits_t::difference_type difference_type; + typedef typename detail::if_c + ::type pointer; + typedef typename detail::if_c + ::type reference; + typedef iterator + < Category + , value_type + , difference_type + , pointer + , reference + > iterator_traits; + typedef typename value_traits_pointers + ::value_traits_ptr value_traits_ptr; + typedef typename value_traits_pointers + ::const_value_traits_ptr const_value_traits_ptr; + static const bool stateful_value_traits = + detail::is_stateful_value_traits::value; +}; + +template +struct iiterator_members +{ + + iiterator_members() + : nodeptr_()//Value initialization to achieve "null iterators" (N3644) + {} + + iiterator_members(const NodePtr &n_ptr, const StoredPointer &data) + : nodeptr_(n_ptr), ptr_(data) + {} + + StoredPointer get_ptr() const + { return ptr_; } + + NodePtr nodeptr_; + StoredPointer ptr_; +}; + +template +struct iiterator_members +{ + iiterator_members() + : nodeptr_()//Value initialization to achieve "null iterators" (N3644) + {} + + iiterator_members(const NodePtr &n_ptr, const StoredPointer &) + : nodeptr_(n_ptr) + {} + + StoredPointer get_ptr() const + { return StoredPointer(); } + + NodePtr nodeptr_; +}; + +namespace detail { + +template inline +void advance_impl(InputIt& it, Distance n, const std::input_iterator_tag&) +{ + while(n--) + ++it; +} + +template inline +void advance_impl(InputIt& it, Distance n, std::forward_iterator_tag &) +{ + while(n--) + ++it; +} + +template inline +void advance_impl(InputIt& it, Distance n, std::bidirectional_iterator_tag &) +{ + for (; 0 < n; --n) + ++it; + for (; n < 0; ++n) + --it; +} + +template +inline void advance_impl(InputIt& it, Distance n, const std::random_access_iterator_tag &) +{ + it += n; +} + +} //namespace detail + +template +inline void iterator_advance(InputIt& it, Distance n) +{ // increment iterator by offset, arbitrary iterators + boost::intrusive::detail::advance_impl(it, n, boost::intrusive::iterator_traits::iterator_category()); +} + +namespace detail{ + +template +inline void distance_impl(InputIt first, InputIt last, Distance& off, const Category &) +{ + while(first != last){ + ++off; + ++first; + } +} + +template inline +void distance_impl(InputIt first, InputIt last, Distance& off, const std::random_access_iterator_tag&) +{ + off += last - first; +} + +} //namespace detail + +template inline +typename iterator_traits::difference_type iterator_distance(InputIt first, InputIt last) +{ + typename iterator_traits::difference_type off = 0; + boost::intrusive::detail::distance_impl(first, last, off, boost::intrusive::iterator_traits::iterator_category()); + return off; +} + + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_IITERATOR_HPP diff --git a/boost/intrusive/detail/is_stateful_value_traits.hpp b/boost/intrusive/detail/is_stateful_value_traits.hpp new file mode 100644 index 0000000..da2260e --- /dev/null +++ b/boost/intrusive/detail/is_stateful_value_traits.hpp @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2009-2013. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP +#define BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#if defined(_MSC_VER) && (_MSC_VER <= 1310) + +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct is_stateful_value_traits +{ + static const bool value = !detail::is_empty_class::value; +}; + +}}} + +#else + +#include + +BOOST_INTRUSIVE_CREATE_FUNCTION_DETECTOR(to_node_ptr, boost_intrusive) +BOOST_INTRUSIVE_CREATE_FUNCTION_DETECTOR(to_value_ptr, boost_intrusive) + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct is_stateful_value_traits +{ + typedef typename ValueTraits::node_ptr node_ptr; + typedef typename ValueTraits::pointer pointer; + typedef typename ValueTraits::value_type value_type; + typedef typename ValueTraits::const_node_ptr const_node_ptr; + typedef typename ValueTraits::const_pointer const_pointer; + + typedef ValueTraits value_traits; + + static const bool value = + (boost::intrusive::function_detector::NonStaticFunction == + (BOOST_INTRUSIVE_DETECT_FUNCTION(ValueTraits, boost_intrusive, node_ptr, to_node_ptr, (value_type&) ))) + || + (boost::intrusive::function_detector::NonStaticFunction == + (BOOST_INTRUSIVE_DETECT_FUNCTION(ValueTraits, boost_intrusive, pointer, to_value_ptr, (node_ptr) ))) + || + (boost::intrusive::function_detector::NonStaticFunction == + (BOOST_INTRUSIVE_DETECT_FUNCTION(ValueTraits, boost_intrusive, const_node_ptr, to_node_ptr, (const value_type&) ))) + || + (boost::intrusive::function_detector::NonStaticFunction == + (BOOST_INTRUSIVE_DETECT_FUNCTION(ValueTraits, boost_intrusive, const_pointer, to_value_ptr, (const_node_ptr) ))) + ; +}; + +}}} + +#endif + +#endif //@ifndef BOOST_INTRUSIVE_DETAIL_IS_STATEFUL_VALUE_TRAITS_HPP diff --git a/boost/intrusive/detail/key_nodeptr_comp.hpp b/boost/intrusive/detail/key_nodeptr_comp.hpp new file mode 100644 index 0000000..dfee8b9 --- /dev/null +++ b/boost/intrusive/detail/key_nodeptr_comp.hpp @@ -0,0 +1,72 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_KEY_NODEPTR_COMP_HPP +#define BOOST_INTRUSIVE_DETAIL_KEY_NODEPTR_COMP_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct key_nodeptr_comp + : private ebo_functor_holder +{ + typedef ValueTraits value_traits; + typedef typename value_traits::value_type value_type; + typedef typename value_traits::node_ptr node_ptr; + typedef typename value_traits::const_node_ptr const_node_ptr; + typedef ebo_functor_holder base_t; + + key_nodeptr_comp(KeyValueCompare kcomp, const ValueTraits *traits) + : base_t(kcomp), traits_(traits) + {} + + template + struct is_node_ptr + { + static const bool value = is_same::value || is_same::value; + }; + + template + const value_type & key_forward + (const T &node, typename enable_if_c::value>::type * = 0) const + { return *traits_->to_value_ptr(node); } + + template + const T & key_forward(const T &key, typename enable_if_c::value>::type* = 0) const + { return key; } + + + template + bool operator()(const KeyType &key1, const KeyType2 &key2) const + { return base_t::get()(this->key_forward(key1), this->key_forward(key2)); } + + template + bool operator()(const KeyType &key1) const + { return base_t::get()(this->key_forward(key1)); } + + const ValueTraits *const traits_; +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_KEY_NODEPTR_COMP_HPP diff --git a/boost/intrusive/detail/list_iterator.hpp b/boost/intrusive/detail/list_iterator.hpp new file mode 100644 index 0000000..efddfba --- /dev/null +++ b/boost/intrusive/detail/list_iterator.hpp @@ -0,0 +1,129 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_LIST_ITERATOR_HPP +#define BOOST_INTRUSIVE_LIST_ITERATOR_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { + +// list_iterator provides some basic functions for a +// node oriented bidirectional iterator: +template +class list_iterator +{ + protected: + typedef iiterator + types_t; + + static const bool stateful_value_traits = types_t::stateful_value_traits; + + typedef ValueTraits value_traits; + typedef typename types_t::node_traits node_traits; + + typedef typename types_t::node node; + typedef typename types_t::node_ptr node_ptr; + typedef typename types_t::const_value_traits_ptr const_value_traits_ptr; + + public: + typedef typename types_t::iterator_traits::difference_type difference_type; + typedef typename types_t::iterator_traits::value_type value_type; + typedef typename types_t::iterator_traits::pointer pointer; + typedef typename types_t::iterator_traits::reference reference; + typedef typename types_t::iterator_traits::iterator_category iterator_category; + + list_iterator() + {} + + explicit list_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr) + : members_(nodeptr, traits_ptr) + {} + + list_iterator(list_iterator const& other) + : members_(other.pointed_node(), other.get_value_traits()) + {} + + const node_ptr &pointed_node() const + { return members_.nodeptr_; } + + list_iterator &operator=(const node_ptr &node) + { members_.nodeptr_ = node; return static_cast(*this); } + + const_value_traits_ptr get_value_traits() const + { return members_.get_ptr(); } + + public: + list_iterator& operator++() + { + node_ptr p = node_traits::get_next(members_.nodeptr_); + members_.nodeptr_ = p; + return static_cast (*this); + } + + list_iterator operator++(int) + { + list_iterator result (*this); + members_.nodeptr_ = node_traits::get_next(members_.nodeptr_); + return result; + } + + list_iterator& operator--() + { + members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); + return static_cast (*this); + } + + list_iterator operator--(int) + { + list_iterator result (*this); + members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_); + return result; + } + + friend bool operator== (const list_iterator& l, const list_iterator& r) + { return l.pointed_node() == r.pointed_node(); } + + friend bool operator!= (const list_iterator& l, const list_iterator& r) + { return !(l == r); } + + reference operator*() const + { return *operator->(); } + + pointer operator->() const + { return this->operator_arrow(detail::bool_()); } + + list_iterator unconst() const + { return list_iterator(this->pointed_node(), this->get_value_traits()); } + + private: + pointer operator_arrow(detail::false_) const + { return ValueTraits::to_value_ptr(members_.nodeptr_); } + + pointer operator_arrow(detail::true_) const + { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); } + + iiterator_members members_; +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_LIST_ITERATOR_HPP diff --git a/boost/intrusive/detail/list_node.hpp b/boost/intrusive/detail/list_node.hpp new file mode 100644 index 0000000..ba97ece --- /dev/null +++ b/boost/intrusive/detail/list_node.hpp @@ -0,0 +1,67 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_LIST_NODE_HPP +#define BOOST_INTRUSIVE_LIST_NODE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace intrusive { + +// list_node_traits can be used with circular_list_algorithms and supplies +// a list_node holding the pointers needed for a double-linked list +// it is used by list_derived_node and list_member_node + +template +struct list_node +{ + typedef typename pointer_rebind::type node_ptr; + node_ptr next_; + node_ptr prev_; +}; + +template +struct list_node_traits +{ + typedef list_node node; + typedef typename node::node_ptr node_ptr; + typedef typename pointer_rebind::type const_node_ptr; + + static node_ptr get_previous(const const_node_ptr & n) + { return n->prev_; } + + static node_ptr get_previous(const node_ptr & n) + { return n->prev_; } + + static void set_previous(const node_ptr & n, const node_ptr & prev) + { n->prev_ = prev; } + + static node_ptr get_next(const const_node_ptr & n) + { return n->next_; } + + static node_ptr get_next(const node_ptr & n) + { return n->next_; } + + static void set_next(const node_ptr & n, const node_ptr & next) + { n->next_ = next; } +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_LIST_NODE_HPP diff --git a/boost/intrusive/detail/math.hpp b/boost/intrusive/detail/math.hpp new file mode 100644 index 0000000..f212ae7 --- /dev/null +++ b/boost/intrusive/detail/math.hpp @@ -0,0 +1,271 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_MATH_HPP +#define BOOST_INTRUSIVE_DETAIL_MATH_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +/////////////////////////// +// floor_log2 Dispatcher +//////////////////////////// + +#if defined(_MSC_VER) && (_MSC_VER >= 1300) + + }}} //namespace boost::intrusive::detail + + //Use _BitScanReverseXX intrinsics + + #if defined(_M_X64) || defined(_M_AMD64) || defined(_M_IA64) //64 bit target + #define BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #endif + + #ifndef __INTRIN_H_ // Avoid including any windows system header + #ifdef __cplusplus + extern "C" { + #endif // __cplusplus + + #if defined(BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT) //64 bit target + unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask); + #pragma intrinsic(_BitScanReverse64) + #else //32 bit target + unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); + #pragma intrinsic(_BitScanReverse) + #endif + + #ifdef __cplusplus + } + #endif // __cplusplus + #endif // __INTRIN_H_ + + #ifdef BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #define BOOST_INTRUSIVE_BSR_INTRINSIC _BitScanReverse64 + #undef BOOST_INTRUSIVE_BSR_INTRINSIC_64_BIT + #else + #define BOOST_INTRUSIVE_BSR_INTRINSIC _BitScanReverse + #endif + + namespace boost { + namespace intrusive { + namespace detail { + + inline std::size_t floor_log2 (std::size_t x) + { + unsigned long log2; + BOOST_INTRUSIVE_BSR_INTRINSIC( &log2, (unsigned long)x ); + return log2; + } + + #undef BOOST_INTRUSIVE_BSR_INTRINSIC + +#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) //GCC >=3.4 + + //Compile-time error in case of missing specialization + template + struct builtin_clz_dispatch; + + #if defined(BOOST_HAS_LONG_LONG) + template<> + struct builtin_clz_dispatch + { + static unsigned long long call(unsigned long long n) + { return __builtin_clzll(n); } + }; + #endif + + template<> + struct builtin_clz_dispatch + { + static unsigned long call(unsigned long n) + { return __builtin_clzl(n); } + }; + + template<> + struct builtin_clz_dispatch + { + static unsigned int call(unsigned int n) + { return __builtin_clz(n); } + }; + + inline std::size_t floor_log2(std::size_t n) + { + return sizeof(std::size_t)*CHAR_BIT - std::size_t(1) - builtin_clz_dispatch::call(n); + } + +#else //Portable methods + +//////////////////////////// +// Generic method +//////////////////////////// + + inline std::size_t floor_log2_get_shift(std::size_t n, true_ )//power of two size_t + { return n >> 1; } + + inline std::size_t floor_log2_get_shift(std::size_t n, false_ )//non-power of two size_t + { return (n >> 1) + ((n & 1u) & (n != 1)); } + + template + inline std::size_t floor_log2 (std::size_t x, integer) + { + const std::size_t Bits = N; + const bool Size_t_Bits_Power_2= !(Bits & (Bits-1)); + + std::size_t n = x; + std::size_t log2 = 0; + + std::size_t remaining_bits = Bits; + std::size_t shift = floor_log2_get_shift(remaining_bits, bool_()); + while(shift){ + std::size_t tmp = n >> shift; + if (tmp){ + log2 += shift, n = tmp; + } + shift = floor_log2_get_shift(shift, bool_()); + } + + return log2; + } + + //////////////////////////// + // DeBruijn method + //////////////////////////// + + //Taken from: + //http://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers + //Thanks to Desmond Hume + + inline std::size_t floor_log2 (std::size_t v, integer) + { + static const int MultiplyDeBruijnBitPosition[32] = + { + 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 + }; + + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + + return MultiplyDeBruijnBitPosition[(std::size_t)(v * 0x07C4ACDDU) >> 27]; + } + + inline std::size_t floor_log2 (std::size_t v, integer) + { + static const std::size_t MultiplyDeBruijnBitPosition[64] = { + 63, 0, 58, 1, 59, 47, 53, 2, + 60, 39, 48, 27, 54, 33, 42, 3, + 61, 51, 37, 40, 49, 18, 28, 20, + 55, 30, 34, 11, 43, 14, 22, 4, + 62, 57, 46, 52, 38, 26, 32, 41, + 50, 36, 17, 19, 29, 10, 13, 21, + 56, 45, 25, 31, 35, 16, 9, 12, + 44, 24, 15, 8, 23, 7, 6, 5}; + + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + return MultiplyDeBruijnBitPosition[((std::size_t)((v - (v >> 1))*0x07EDD5E59A4E28C2ULL)) >> 58]; + } + + + inline std::size_t floor_log2 (std::size_t x) + { + const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT; + return floor_log2(x, integer()); + } + +#endif + +//Thanks to Laurent de Soras in +//http://www.flipcode.com/archives/Fast_log_Function.shtml +inline float fast_log2 (float val) +{ + union caster_t + { + unsigned x; + float val; + } caster; + + caster.val = val; + unsigned x = caster.x; + const int log_2 = int((x >> 23) & 255) - 128; + x &= ~(unsigned(255u) << 23u); + x += unsigned(127) << 23u; + caster.x = x; + val = caster.val; + //1+log2(m), m ranging from 1 to 2 + //3rd degree polynomial keeping first derivate continuity. + //For less precision the line can be commented out + val = ((-1.f/3.f) * val + 2.f) * val - (2.f/3.f); + return val + static_cast(log_2); +} + +inline std::size_t ceil_log2 (std::size_t x) +{ + return static_cast((x & (x-1)) != 0) + floor_log2(x); +} + +template +struct numbits_eq +{ + static const bool value = sizeof(SizeType)*CHAR_BIT == N; +}; + +template +struct sqrt2_pow_max; + +template +struct sqrt2_pow_max >::type> +{ + static const SizeType value = 0xb504f334; + static const std::size_t pow = 31; +}; + +#ifndef BOOST_NO_INT64_T + +template +struct sqrt2_pow_max >::type> +{ + static const SizeType value = 0xb504f333f9de6484ull; + static const std::size_t pow = 63; +}; + +#endif //BOOST_NO_INT64_T + +// Returns floor(pow(sqrt(2), x * 2 + 1)). +// Defined for X from 0 up to the number of bits in size_t minus 1. +inline std::size_t sqrt2_pow_2xplus1 (std::size_t x) +{ + const std::size_t value = (std::size_t)sqrt2_pow_max::value; + const std::size_t pow = (std::size_t)sqrt2_pow_max::pow; + return (value >> (pow - x)) + 1; +} + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_DETAIL_MATH_HPP diff --git a/boost/intrusive/detail/memory_util.hpp b/boost/intrusive/detail/memory_util.hpp new file mode 100644 index 0000000..18a5d3e --- /dev/null +++ b/boost/intrusive/detail/memory_util.hpp @@ -0,0 +1,92 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Pablo Halpern 2009. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2011-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_ALLOCATOR_MEMORY_UTIL_HPP +#define BOOST_INTRUSIVE_ALLOCATOR_MEMORY_UTIL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +inline T* addressof(T& obj) +{ + return static_cast + (static_cast + (const_cast + (&reinterpret_cast(obj)) + ) + ); +} + +template +struct LowPriorityConversion +{ + // Convertible from T with user-defined-conversion rank. + LowPriorityConversion(const T&) { } +}; + +}}} //namespace boost::intrusive::detail + +#include + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME pointer_to +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME static_cast_from +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME const_cast_from +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME dynamic_cast_from +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEGIN namespace boost { namespace intrusive { namespace detail { +#define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}} +#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 1, )) +#include BOOST_PP_ITERATE() + +namespace boost { +namespace intrusive { +namespace detail { + +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(element_type) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(difference_type) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(reference) +BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(value_traits_ptr) + +} //namespace detail { +} //namespace intrusive { +} //namespace boost { + +#endif // ! defined(BOOST_INTRUSIVE_ALLOCATOR_MEMORY_UTIL_HPP) diff --git a/boost/intrusive/detail/mpl.hpp b/boost/intrusive/detail/mpl.hpp new file mode 100644 index 0000000..9b2c9f1 --- /dev/null +++ b/boost/intrusive/detail/mpl.hpp @@ -0,0 +1,376 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2014 +// (C) Copyright Microsoft Corporation 2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_MPL_HPP +#define BOOST_INTRUSIVE_DETAIL_MPL_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct is_same +{ + static const bool value = false; +}; + +template +struct is_same +{ + static const bool value = true; +}; + +template +struct add_const +{ typedef const T type; }; + +template +struct remove_const +{ typedef T type; }; + +template +struct remove_const +{ typedef T type; }; + +template +struct remove_cv +{ typedef T type; }; + +template +struct remove_cv +{ typedef T type; }; + +template +struct remove_cv +{ typedef T type; }; + +template +struct remove_cv +{ typedef T type; }; + +template +struct remove_reference +{ + typedef T type; +}; + +template +struct remove_reference +{ + typedef T type; +}; + +template +struct remove_pointer +{ + typedef T type; +}; + +template +struct remove_pointer +{ + typedef T type; +}; + +template +struct add_pointer +{ + typedef T *type; +}; + +typedef char one; +struct two {one _[2];}; + +template< bool C_ > +struct bool_ +{ + static const bool value = C_; +}; + +template< class Integer, Integer Value > +struct integer +{ + static const Integer value = Value; +}; + +typedef bool_ true_; +typedef bool_ false_; + +typedef true_ true_type; +typedef false_ false_type; + +typedef char yes_type; +struct no_type +{ + char padding[8]; +}; + +template +struct enable_if_c { + typedef T type; +}; + +template +struct enable_if_c {}; + +template +struct enable_if : public enable_if_c{}; + +template +struct apply +{ + typedef typename F::template apply::type type; +}; + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) + +template +struct is_convertible +{ + static const bool value = __is_convertible_to(T, U); +}; + +#else + +template +class is_convertible +{ + typedef char true_t; + class false_t { char dummy[2]; }; + //use any_conversion as first parameter since in MSVC + //overaligned types can't go through ellipsis + static false_t dispatch(...); + static true_t dispatch(U); + static typename remove_reference::type &trigger(); + public: + static const bool value = sizeof(dispatch(trigger())) == sizeof(true_t); +}; + +#endif + +template< + bool C + , typename T1 + , typename T2 + > +struct if_c +{ + typedef T1 type; +}; + +template< + typename T1 + , typename T2 + > +struct if_c +{ + typedef T2 type; +}; + +template< + typename C + , typename T1 + , typename T2 + > +struct if_ +{ + typedef typename if_c<0 != C::value, T1, T2>::type type; +}; + +template< + bool C + , typename F1 + , typename F2 + > +struct eval_if_c + : if_c::type +{}; + +template< + typename C + , typename T1 + , typename T2 + > +struct eval_if + : if_::type +{}; + +// identity is an extension: it is not part of the standard. +template +struct identity +{ + typedef T type; +}; + +template +struct add_const_if_c +{ + typedef typename if_c + < Add + , typename add_const::type + , T + >::type type; +}; + + +//boost::alignment_of yields to 10K lines of preprocessed code, so we +//need an alternative +template struct alignment_of; + +template +struct alignment_of_hack +{ + char c; + T t; + alignment_of_hack(); +}; + +template +struct alignment_logic +{ + static const std::size_t value = A < S ? A : S; +}; + +template< typename T > +struct alignment_of +{ + static const std::size_t value = alignment_logic + < sizeof(alignment_of_hack) - sizeof(T) + , sizeof(T) + >::value; +}; + +template +class is_empty_class +{ + template + struct empty_helper_t1 : public T + { + empty_helper_t1(); + int i[256]; + }; + + struct empty_helper_t2 + { int i[256]; }; + + public: + static const bool value = sizeof(empty_helper_t1) == sizeof(empty_helper_t2); +}; + +template +struct ls_zeros +{ + static const std::size_t value = (S & std::size_t(1)) ? 0 : (1 + ls_zeros<(S>>1u)>::value); +}; + +template<> +struct ls_zeros<0> +{ + static const std::size_t value = 0; +}; + +template<> +struct ls_zeros<1> +{ + static const std::size_t value = 0; +}; + +template struct unvoid_ref { typedef T &type; }; +template <> struct unvoid_ref { struct type_impl { }; typedef type_impl & type; }; +template <> struct unvoid_ref { struct type_impl { }; typedef type_impl & type; }; + +// Infrastructure for providing a default type for T::TNAME if absent. +#define BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(TNAME) \ + template \ + struct boost_intrusive_default_type_ ## TNAME \ + { \ + template \ + static char test(int, typename X::TNAME*); \ + \ + template \ + static int test(...); \ + \ + struct DefaultWrap { typedef DefaultType TNAME; }; \ + \ + static const bool value = (1 == sizeof(test(0, 0))); \ + \ + typedef typename \ + ::boost::intrusive::detail::if_c \ + ::type::TNAME type; \ + }; \ + \ + template \ + struct boost_intrusive_eval_default_type_ ## TNAME \ + { \ + template \ + static char test(int, typename X::TNAME*); \ + \ + template \ + static int test(...); \ + \ + struct DefaultWrap \ + { typedef typename DefaultType::type TNAME; }; \ + \ + static const bool value = (1 == sizeof(test(0, 0))); \ + \ + typedef typename \ + ::boost::intrusive::detail::eval_if_c \ + < value \ + , ::boost::intrusive::detail::identity \ + , ::boost::intrusive::detail::identity \ + >::type::TNAME type; \ + }; \ +// + +#define BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(INSTANTIATION_NS_PREFIX, T, TNAME, TIMPL) \ + typename INSTANTIATION_NS_PREFIX \ + boost_intrusive_default_type_ ## TNAME< T, TIMPL >::type \ +// + +#define BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(INSTANTIATION_NS_PREFIX, T, TNAME, TIMPL) \ + typename INSTANTIATION_NS_PREFIX \ + boost_intrusive_eval_default_type_ ## TNAME< T, TIMPL >::type \ +// + +#define BOOST_INTRUSIVE_INTERNAL_STATIC_BOOL_IS_TRUE(TRAITS_PREFIX, TYPEDEF_TO_FIND) \ +template \ +struct TRAITS_PREFIX##_bool\ +{\ + template\ + struct two_or_three {one _[2 + Add];};\ + template static one test(...);\ + template static two_or_three test (int);\ + static const std::size_t value = sizeof(test(0));\ +};\ +\ +template \ +struct TRAITS_PREFIX##_bool_is_true\ +{\ + static const bool value = TRAITS_PREFIX##_bool::value > sizeof(one)*2;\ +};\ +// + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_DETAIL_MPL_HPP diff --git a/boost/intrusive/detail/node_cloner_disposer.hpp b/boost/intrusive/detail/node_cloner_disposer.hpp new file mode 100644 index 0000000..2a18100 --- /dev/null +++ b/boost/intrusive/detail/node_cloner_disposer.hpp @@ -0,0 +1,109 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_NODE_CLONER_DISPOSER_HPP +#define BOOST_INTRUSIVE_DETAIL_NODE_CLONER_DISPOSER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct node_cloner + : private ebo_functor_holder +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node_ptr node_ptr; + typedef ebo_functor_holder base_t; + typedef typename get_algo< AlgoType + , node_traits>::type node_algorithms; + static const bool safemode_or_autounlink = + is_safe_autounlink::value; + typedef typename value_traits::value_type value_type; + typedef typename value_traits::pointer pointer; + typedef typename node_traits::node node; + typedef typename value_traits::const_node_ptr const_node_ptr; + typedef typename value_traits::reference reference; + typedef typename value_traits::const_reference const_reference; + + node_cloner(F f, const ValueTraits *traits) + : base_t(f), traits_(traits) + {} + + // tree-based containers use this method, which is proxy-reference friendly + node_ptr operator()(const node_ptr & p) + { + const_reference v = *traits_->to_value_ptr(p); + node_ptr n = traits_->to_node_ptr(*base_t::get()(v)); + //Cloned node must be in default mode if the linking mode requires it + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); + return n; + } + + // hashtables use this method, which is proxy-reference unfriendly + node_ptr operator()(const node &to_clone) + { + const value_type &v = + *traits_->to_value_ptr + (pointer_traits::pointer_to(to_clone)); + node_ptr n = traits_->to_node_ptr(*base_t::get()(v)); + //Cloned node must be in default mode if the linking mode requires it + if(safemode_or_autounlink) + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); + return n; + } + + const ValueTraits * const traits_; +}; + +template +struct node_disposer + : private ebo_functor_holder +{ + typedef ValueTraits value_traits; + typedef typename value_traits::node_traits node_traits; + typedef typename node_traits::node_ptr node_ptr; + typedef ebo_functor_holder base_t; + typedef typename get_algo< AlgoType + , node_traits>::type node_algorithms; + static const bool safemode_or_autounlink = + is_safe_autounlink::value; + + node_disposer(F f, const ValueTraits *cont) + : base_t(f), traits_(cont) + {} + + void operator()(const node_ptr & p) + { + if(safemode_or_autounlink) + node_algorithms::init(p); + base_t::get()(traits_->to_value_ptr(p)); + } + const ValueTraits * const traits_; +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_NODE_CLONER_DISPOSER_HPP diff --git a/boost/intrusive/detail/node_holder.hpp b/boost/intrusive/detail/node_holder.hpp new file mode 100644 index 0000000..12732a8 --- /dev/null +++ b/boost/intrusive/detail/node_holder.hpp @@ -0,0 +1,31 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_NODE_HOLDER_HPP +#define BOOST_INTRUSIVE_DETAIL_NODE_HOLDER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +namespace boost { +namespace intrusive { + +template +struct node_holder + : public Node +{}; + +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_NODE_HOLDER_HPP diff --git a/boost/intrusive/detail/node_to_value.hpp b/boost/intrusive/detail/node_to_value.hpp new file mode 100644 index 0000000..6ec3971 --- /dev/null +++ b/boost/intrusive/detail/node_to_value.hpp @@ -0,0 +1,126 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP +#define BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +struct dummy_constptr +{ + typedef typename boost::intrusive::pointer_traits:: + template rebind_pointer::type ConstVoidPtr; + + explicit dummy_constptr(ConstVoidPtr) + {} + + dummy_constptr() + {} + + ConstVoidPtr get_ptr() const + { return ConstVoidPtr(); } +}; + +template +struct constptr +{ + typedef typename boost::intrusive::pointer_traits:: + template rebind_pointer::type ConstVoidPtr; + + constptr() + {} + + explicit constptr(const ConstVoidPtr &ptr) + : const_void_ptr_(ptr) + {} + + const void *get_ptr() const + { return boost::intrusive::detail::to_raw_pointer(const_void_ptr_); } + + ConstVoidPtr const_void_ptr_; +}; + +template +struct select_constptr +{ + typedef typename if_c + < store_ptr + , constptr + , dummy_constptr + >::type type; +}; + + +template +struct node_to_value + : public select_constptr + < typename pointer_traits + ::template rebind_pointer::type + , is_stateful_value_traits::value + >::type +{ + static const bool stateful_value_traits = is_stateful_value_traits::value; + typedef typename select_constptr + < typename pointer_traits + :: + template rebind_pointer::type + , stateful_value_traits >::type Base; + + typedef ValueTraits value_traits; + typedef typename value_traits::value_type value_type; + typedef typename value_traits::node_traits::node node; + typedef typename add_const_if_c + ::type vtype; + typedef typename add_const_if_c + ::type ntype; + typedef typename pointer_traits + :: + template rebind_pointer::type npointer; + typedef typename pointer_traits:: + template rebind_pointer::type const_value_traits_ptr; + + node_to_value(const const_value_traits_ptr &ptr) + : Base(ptr) + {} + + typedef vtype & result_type; + typedef ntype & first_argument_type; + + const_value_traits_ptr get_value_traits() const + { return pointer_traits::static_cast_from(Base::get_ptr()); } + + result_type to_value(first_argument_type arg, false_) const + { return *(value_traits::to_value_ptr(pointer_traits::pointer_to(arg))); } + + result_type to_value(first_argument_type arg, true_) const + { return *(this->get_value_traits()->to_value_ptr(pointer_traits::pointer_to(arg))); } + + result_type operator()(first_argument_type arg) const + { return this->to_value(arg, bool_()); } +}; + +} //namespace detail{ +} //namespace intrusive{ +} //namespace boost{ + +#endif //BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP diff --git a/boost/intrusive/detail/parent_from_member.hpp b/boost/intrusive/detail/parent_from_member.hpp new file mode 100644 index 0000000..3dfe8d6 --- /dev/null +++ b/boost/intrusive/detail/parent_from_member.hpp @@ -0,0 +1,120 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2013 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP +#define BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include + +#if defined(BOOST_MSVC) || ((defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) && defined(BOOST_INTEL)) + #define BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER + #include +#endif + +namespace boost { +namespace intrusive { +namespace detail { + +template +inline std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member) +{ + //The implementation of a pointer to member is compiler dependent. + #if defined(BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER) + + //MSVC compliant compilers use their the first 32 bits as offset (even in 64 bit mode) + union caster_union + { + const Member Parent::* ptr_to_member; + int offset; + } caster; + + //MSVC ABI can use up to 3 int32 to represent pointer to member data + //with virtual base classes, in those cases there is no simple to + //obtain the address of the parent. So static assert to avoid runtime errors + BOOST_STATIC_ASSERT( sizeof(caster) == sizeof(int) ); + + caster.ptr_to_member = ptr_to_member; + return std::ptrdiff_t(caster.offset); + //Additional info on MSVC behaviour for the future. For 2/3 int ptr-to-member + //types dereference seems to be: + // + // vboffset = [compile_time_offset if 2-int ptr2memb] / + // [ptr2memb.i32[2] if 3-int ptr2memb]. + // vbtable = *(this + vboffset); + // adj = vbtable[ptr2memb.i32[1]]; + // var = adj + (this + vboffset) + ptr2memb.i32[0]; + // + //To reverse the operation we need to + // - obtain vboffset (in 2-int ptr2memb implementation only) + // - Go to Parent's vbtable and obtain adjustment at index ptr2memb.i32[1] + // - parent = member - adj - vboffset - ptr2memb.i32[0] + // + //Even accessing to RTTI we might not be able to obtain this information + //so anyone who thinks it's possible, please send a patch. + + //This works with gcc, msvc, ac++, ibmcpp + #elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || \ + defined(__IBMCPP__) || defined(__DECCXX) + const Parent * const parent = 0; + const char *const member = static_cast(static_cast(&(parent->*ptr_to_member))); + return std::ptrdiff_t(member - static_cast(static_cast(parent))); + #else + //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC + union caster_union + { + const Member Parent::* ptr_to_member; + std::ptrdiff_t offset; + } caster; + caster.ptr_to_member = ptr_to_member; + return caster.offset - 1; + #endif +} + +template +inline Parent *parent_from_member(Member *member, const Member Parent::* ptr_to_member) +{ + return static_cast + ( + static_cast + ( + static_cast(static_cast(member)) - offset_from_pointer_to_member(ptr_to_member) + ) + ); +} + +template +inline const Parent *parent_from_member(const Member *member, const Member Parent::* ptr_to_member) +{ + return static_cast + ( + static_cast + ( + static_cast(static_cast(member)) - offset_from_pointer_to_member(ptr_to_member) + ) + ); +} + +} //namespace detail { +} //namespace intrusive { +} //namespace boost { + +#ifdef BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER +#undef BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER +#endif + +#include + +#endif //#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP diff --git a/boost/intrusive/detail/pointer_element.hpp b/boost/intrusive/detail/pointer_element.hpp new file mode 100644 index 0000000..1c17f41 --- /dev/null +++ b/boost/intrusive/detail/pointer_element.hpp @@ -0,0 +1,170 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_DETAIL_POINTER_ELEMENT_HPP +#define BOOST_INTRUSIVE_DETAIL_POINTER_ELEMENT_HPP + +#if defined(_MSC_VER) +# pragma once +#endif + +#include + +namespace boost { +namespace intrusive { +namespace detail{ + +////////////////////// +//struct first_param +////////////////////// + +template struct first_param +{ typedef void type; }; + +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + + template