Add experimental small_vector class

This commit is contained in:
Ion Gaztañaga
2015-02-26 00:35:59 +01:00
parent 4609c78ccb
commit 8cdfec7890
9 changed files with 1068 additions and 132 deletions

View File

@@ -1086,6 +1086,7 @@ use [*Boost.Container]? There are several reasons for that:
and [*Boost.Intrusive]. Preprocessed code size have decreased considerably and compilation times have improved. and [*Boost.Intrusive]. Preprocessed code size have decreased considerably and compilation times have improved.
* Added `nth` and `index_of` functions to containers with random-access iterators (except `basic_string`). * Added `nth` and `index_of` functions to containers with random-access iterators (except `basic_string`).
* Added C++17's `allocator_traits<Allocator>::is_always_equal`. * Added C++17's `allocator_traits<Allocator>::is_always_equal`.
* Experimental [classref boost::container::small_vector small_vector] container.
* Updated containers to implement new constructors as specified in * Updated containers to implement new constructors as specified in
[@http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2210 2210. Missing allocator-extended constructor for allocator-aware containers]. [@http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2210 2210. Missing allocator-extended constructor for allocator-aware containers].
* Fixed bugs: * Fixed bugs:

View File

@@ -98,6 +98,10 @@ class stable_vector;
template <class T, std::size_t Capacity> template <class T, std::size_t Capacity>
class static_vector; class static_vector;
template < class T, std::size_t N
, class Allocator= new_allocator<T> >
class small_vector;
template <class T template <class T
,class Allocator = new_allocator<T> > ,class Allocator = new_allocator<T> >
class deque; class deque;

View File

@@ -0,0 +1,494 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. 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_SMALL_VECTOR_HPP
#define BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
// container
#include <boost/container/container_fwd.hpp>
#include <boost/container/vector.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/container/new_allocator.hpp> //new_allocator
// container/detail
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/detail/version_type.hpp>
//move
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/iterator.hpp>
//move/detail
#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <boost/move/detail/fwd_macros.hpp>
#endif
//std
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
#include <initializer_list> //for std::initializer_list
#endif
namespace boost {
namespace container {
template <class T, class Allocator = new_allocator<T> >
class small_vector_base;
/////////////////////////////////////////////////////
//
// small_vector_allocator
//
/////////////////////////////////////////////////////
template<class Allocator>
class small_vector_allocator
: public Allocator
{
typedef unsigned int allocation_type;
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
//Self type
typedef small_vector_allocator<Allocator> self_t;
BOOST_COPYABLE_AND_MOVABLE(small_vector_allocator)
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
const Allocator &as_base() const
{ return static_cast<const Allocator&>(*this); }
Allocator &as_base()
{ return static_cast<Allocator&>(*this); }
public:
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
typedef allocator_traits<Allocator> allocator_traits_type;
typedef typename
container_detail::version<Allocator>::type version;
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
typedef typename allocator_traits<Allocator>::value_type value_type;
typedef typename allocator_traits<Allocator>::pointer pointer;
typedef typename allocator_traits<Allocator>::const_pointer const_pointer;
typedef typename allocator_traits<Allocator>::reference reference;
typedef typename allocator_traits<Allocator>::const_reference const_reference;
typedef typename allocator_traits<Allocator>::size_type size_type;
typedef typename allocator_traits<Allocator>::difference_type difference_type;
typedef typename allocator_traits<Allocator>::void_pointer void_pointer;
typedef typename allocator_traits<Allocator>::const_void_pointer const_void_pointer;
typedef typename allocator_traits_type::propagate_on_container_copy_assignment propagate_on_container_copy_assignment;
typedef typename allocator_traits_type::propagate_on_container_move_assignment propagate_on_container_move_assignment;
typedef typename allocator_traits_type::propagate_on_container_swap propagate_on_container_swap;
typedef container_detail::bool_<false> is_always_equal;
typedef container_detail::bool_<true> is_partially_propagable;
//!Obtains an small_vector_allocator that allocates
//!objects of type T2
template<class T2>
struct rebind
{
typedef typename allocator_traits_type::template rebind_alloc<T2>::type other;
};
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//!Constructor from arbitrary arguments
template<class ...Args>
explicit small_vector_allocator(BOOST_FWD_REF(Args) ...args)
: Allocator(::boost::forward<Args>(args)...)
{}
#else
#define BOOST_CONTAINER_SMALL_VECTOR_ALLOCATOR_CTOR_CODE(N) \
BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
explicit small_vector_allocator(BOOST_MOVE_UREF##N)\
: Allocator(BOOST_MOVE_FWD##N)\
{}\
//
BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SMALL_VECTOR_ALLOCATOR_CTOR_CODE)
#undef BOOST_CONTAINER_SMALL_VECTOR_ALLOCATOR_CTOR_CODE
#endif
//!Constructor from other small_vector_allocator.
//!Never throws
small_vector_allocator(const small_vector_allocator &other) BOOST_NOEXCEPT_OR_NOTHROW
: Allocator(other.as_base())
{}
//!Move constructor from small_vector_allocator.
//!Never throws
small_vector_allocator(BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
: Allocator(::boost::move(other.as_base()))
{}
//!Constructor from related small_vector_allocator.
//!Never throws
template<class OtherAllocator>
small_vector_allocator(const small_vector_allocator<OtherAllocator> &other) BOOST_NOEXCEPT_OR_NOTHROW
: Allocator(other.as_base())
{}
//!Move constructor from related small_vector_allocator.
//!Never throws
template<class OtherAllocator>
small_vector_allocator(BOOST_RV_REF(small_vector_allocator<OtherAllocator>) other) BOOST_NOEXCEPT_OR_NOTHROW
: Allocator(::boost::move(other.as_base()))
{}
//!Assignment from other small_vector_allocator.
//!Never throws
small_vector_allocator & operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
{ return static_cast<small_vector_allocator&>(this->Allocator::operator=(other.as_base())); }
//!Move constructor from other small_vector_allocator.
//!Never throws
small_vector_allocator & operator=(BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
{ return static_cast<small_vector_allocator&>(this->Allocator::operator=(::boost::move(other.as_base()))); }
//!Assignment from related small_vector_allocator.
//!Never throws
template<class OtherAllocator>
small_vector_allocator & operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator<OtherAllocator>) other) BOOST_NOEXCEPT_OR_NOTHROW
{ return static_cast<small_vector_allocator&>(this->Allocator::operator=(other.as_base())); }
//!Move assignment from related small_vector_allocator.
//!Never throws
template<class OtherAllocator>
small_vector_allocator & operator=(BOOST_RV_REF(small_vector_allocator<OtherAllocator>) other) BOOST_NOEXCEPT_OR_NOTHROW
{ return static_cast<small_vector_allocator&>(this->Allocator::operator=(::boost::move(other.as_base()))); }
pointer allocate(size_type count, const_void_pointer hint)
{ return allocator_traits_type::allocate(this->as_base(), count, hint); }
//!Deallocates previously allocated memory.
//!Never throws
void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
{
if(!this->is_internal_storage(ptr))
allocator_traits_type::deallocate(this->as_base(), ptr, n);
}
//!Returns the maximum number of elements that could be allocated.
//!Never throws
size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
{ return allocator_traits_type::max_size(this->as_base()); }
small_vector_allocator select_on_container_copy_construction() const
{ return small_vector_allocator(allocator_traits_type::select_on_container_copy_construction(this->as_base())); }
bool storage_can_be_propagated(pointer p, const small_vector_allocator &to, const bool propagate_a) const
{
return !this->is_internal_storage(p) &&
allocator_traits_type::storage_can_be_propagated(this->as_base(), p, to.as_base(), propagate_a);
}
//!Swaps two allocators, does nothing
//!because this small_vector_allocator is stateless
friend void swap(self_t &l, self_t &r) BOOST_NOEXCEPT_OR_NOTHROW
{ boost::adl_move_swap(l.as_base(), r.as_base()); }
//!An small_vector_allocator always compares to true, as memory allocated with one
//!instance can be deallocated by another instance
friend bool operator==(const small_vector_allocator &, const small_vector_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
{ return false; }
//!An small_vector_allocator always compares to false, as memory allocated with one
//!instance can be deallocated by another instance
friend bool operator!=(const small_vector_allocator &, const small_vector_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
{ return true; }
/*
//!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
pointer allocation_command(allocation_type command,
size_type limit_size,
size_type &prefer_in_recvd_out_size,
pointer &reuse)
{ return allocator_traits_type::allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); }
//!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_NOEXCEPT_OR_NOTHROW
{ return allocator_traits_type::size(p); }
*/
private:
//!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
/*
using Allocator::allocate_one;
using Allocator::allocate_individual;
using Allocator::deallocate_one;
using Allocator::deallocate_individual;
using Allocator::allocate_many;
using Allocator::deallocate_many;*/
private:
bool is_internal_storage(pointer p) const
{ return this->internal_storage() == p; }
pointer internal_storage() const
{
typedef typename Allocator::value_type value_type;
typedef container_detail::vector_alloc_holder< small_vector_allocator<Allocator> > vector_alloc_holder_t;
typedef vector<value_type, small_vector_allocator<Allocator> > vector_base;
typedef small_vector_base<value_type, Allocator> derived_type;
//
const vector_alloc_holder_t &v_holder = static_cast<const vector_alloc_holder_t &>(*this);
const vector_base &v_base = reinterpret_cast<const vector_base &>(v_holder);
const derived_type &d_base = static_cast<const derived_type &>(v_base);
return d_base.internal_storage();
}
};
/////////////////////////////////////////////////////
//
// small_vector_base
//
/////////////////////////////////////////////////////
template <class T, class SecondaryAllocator>
class small_vector_base
: public vector<T, small_vector_allocator<SecondaryAllocator> >
{
typedef typename allocator_traits<SecondaryAllocator>::pointer pointer;
BOOST_COPYABLE_AND_MOVABLE(small_vector_base)
public:
typedef vector<T, small_vector_allocator<SecondaryAllocator> > base_type;
typedef typename container_detail::aligned_storage
<sizeof(T), container_detail::alignment_of<T>::value>::type storage_type;
typedef small_vector_allocator<SecondaryAllocator> allocator_type;
pointer internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
{ return boost::intrusive::pointer_traits<pointer>::pointer_to(*const_cast<T*>(static_cast<const T*>(static_cast<const void*>(&m_storage_start)))); }
protected:
base_type &as_base() { return static_cast<base_type&>(*this); }
const base_type &as_base() const { return static_cast<const base_type&>(*this); }
typedef typename base_type::initial_capacity_t initial_capacity_t;
explicit small_vector_base(initial_capacity_t, std::size_t initial_capacity)
: base_type(initial_capacity_t(), this->internal_storage(), initial_capacity)
{}
template<class AllocFwd>
explicit small_vector_base(initial_capacity_t, std::size_t capacity, BOOST_FWD_REF(AllocFwd) a)
: base_type(initial_capacity_t(), this->internal_storage(), capacity, ::boost::forward<AllocFwd>(a))
{}
~small_vector_base(){}
small_vector_base& operator=(BOOST_COPY_ASSIGN_REF(small_vector_base) other)
{ return static_cast<small_vector_base&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
small_vector_base& operator=(BOOST_RV_REF(small_vector_base) other)
{ return static_cast<small_vector_base&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
void swap(small_vector_base &other)
{ return this->base_type::swap(other); }
using base_type::is_propagable_from;
using base_type::steal_resources;
private:
//The only member
storage_type m_storage_start;
};
/////////////////////////////////////////////////////
//
// small_vector_storage_calculator
//
/////////////////////////////////////////////////////
template<std::size_t Needed, std::size_t Hdr, std::size_t SSize, bool NeedsZero = (0u == Needed || Needed <= Hdr)>
struct small_vector_storage_calculator_helper
{
static const std::size_t value = (Needed - Hdr - 1u)/SSize + 1u;
};
template<std::size_t Needed, std::size_t Hdr, std::size_t SSize>
struct small_vector_storage_calculator_helper<Needed, Hdr, SSize, true>
{
static const std::size_t value = 0u;
};
template<class Storage, class Allocator, class T, std::size_t N>
struct small_vector_storage_calculator
{
typedef small_vector_base<T, Allocator> svh_type;
typedef typename svh_type::base_type svhb_type;
static const std::size_t s_align = container_detail::alignment_of<Storage>::value;
static const std::size_t s_size = sizeof(Storage);
static const std::size_t svh_sizeof = sizeof(svh_type);
static const std::size_t svhb_sizeof = sizeof(svhb_type);
static const std::size_t s_start = ((svhb_sizeof-1)/s_align+1)*s_align;
static const std::size_t header_bytes = svh_sizeof-s_start;
static const std::size_t needed_bytes = sizeof(T)*N;
static const std::size_t needed_extra_storages =
small_vector_storage_calculator_helper<needed_bytes, header_bytes, s_size>::value;
};
/////////////////////////////////////////////////////
//
// small_vector_storage_definer
//
/////////////////////////////////////////////////////
template<class Storage, std::size_t N>
struct small_vector_storage
{
Storage m_rest_of_storage[N];
};
template<class Storage>
struct small_vector_storage<Storage, 0>
{};
template<class Allocator, std::size_t N>
struct small_vector_storage_definer
{
typedef typename Allocator::value_type value_type;
typedef typename small_vector_base<value_type, Allocator>::storage_type storage_type;
static const std::size_t needed_extra_storages =
small_vector_storage_calculator<storage_type, Allocator, value_type, N>::needed_extra_storages;
typedef small_vector_storage<storage_type, needed_extra_storages> type;
};
//! small_vector a vector-like container optimized for the case when it contains few elements.
//! It contains some preallocated elements in-place, which allows it to avoid the use of the small_vector_allocator
//! when the actual number of elements is below that preallocated threshold.
//!
//! small_vector<T, N, Allocator> is convertible to small_vector_unbounded<T, Allocator> that is independent
//! from the preallocated element capacity, so client code does not need to be templated on that N argument.
//!
//! \tparam T The type of object that is stored in the small_vector
//! \tparam N The number of preallocated elements stored inside small_vector. It shall be less than Allocator::max_size();
//! \tparam Allocator The small_vector_allocator used for memory management when the number of elements exceeds N.
template <class T, std::size_t N, class Allocator BOOST_CONTAINER_DOCONLY(= new_allocator<T>) >
class small_vector : public small_vector_base<T, Allocator>
, private small_vector_storage_definer<Allocator, N>::type
{
typedef small_vector_base<T, Allocator> base_type;
typedef typename small_vector_storage_definer<Allocator, N>::type remaining_storage_holder;
BOOST_COPYABLE_AND_MOVABLE(small_vector)
typedef typename base_type::initial_capacity_t initial_capacity_t;
typedef allocator_traits<typename base_type::allocator_type> allocator_traits_type;
public:
typedef small_vector_storage_calculator< typename small_vector_base<T, Allocator>
::storage_type, Allocator, T, N> storage_test;
static const std::size_t needed_extra_storages = storage_test::needed_extra_storages;
static const std::size_t needed_bytes = storage_test::needed_bytes;
static const std::size_t header_bytes = storage_test::header_bytes;
static const std::size_t s_start = storage_test::s_start;
typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::size_type size_type;
static std::size_t internal_capacity()
{ return (sizeof(small_vector) - storage_test::s_start)/sizeof(T); }
small_vector()
: base_type(initial_capacity_t(), internal_capacity())
{}
explicit small_vector(size_type n)
: base_type(initial_capacity_t(), internal_capacity())
{ this->resize(n); }
explicit small_vector(const allocator_type &a)
: base_type(initial_capacity_t(), internal_capacity(), a)
{}
small_vector(size_type n, const allocator_type &a)
: base_type(initial_capacity_t(), internal_capacity(), a)
{ this->resize(n); }
small_vector(const small_vector &other)
: base_type( initial_capacity_t(), internal_capacity()
, allocator_traits_type::select_on_container_copy_construction(other.get_stored_allocator()))
{ this->assign(other.cbegin(), other.cend()); }
small_vector(const small_vector &other, const allocator_type &a)
: base_type(initial_capacity_t(), internal_capacity(), a)
{ this->assign(other.cbegin(), other.cend()); }
small_vector(BOOST_RV_REF(small_vector) other)
: base_type(initial_capacity_t(), internal_capacity(), ::boost::move(other.get_stored_allocator()))
{ this->move_construct_impl(other, other.get_stored_allocator()); }
small_vector(BOOST_RV_REF(small_vector) other, const allocator_type &a)
: base_type(initial_capacity_t(), internal_capacity(), a)
{ this->move_construct_impl(other, a); }
small_vector& operator=(BOOST_COPY_ASSIGN_REF(small_vector) other)
{ return static_cast<small_vector&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
small_vector& operator=(BOOST_RV_REF(small_vector) other)
{ return static_cast<small_vector&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
void swap(small_vector &other)
{ return this->base_type::swap(other); }
private:
void move_construct_impl(small_vector &x, const allocator_type &a)
{
if(base_type::is_propagable_from(x, a, true)){
this->steal_resources(x);
}
else{
this->assign( boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.begin()))
, boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.end ()))
);
}
}
};
}}
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
/*
namespace boost {
//!has_trivial_destructor_after_move<> == true_type
//!specialization for optimizations
template <class T, class Allocator>
struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
{
typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
::boost::has_trivial_destructor_after_move<pointer>::value;
};
}
*/
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
#include <boost/container/detail/config_end.hpp>
#endif // #ifndef BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP

View File

@@ -272,6 +272,7 @@ struct vector_alloc_holder
BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder) BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
public: public:
typedef Allocator allocator_type;
typedef boost::container::allocator_traits<Allocator> allocator_traits_type; typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
typedef typename allocator_traits_type::pointer pointer; typedef typename allocator_traits_type::pointer pointer;
typedef typename allocator_traits_type::size_type size_type; typedef typename allocator_traits_type::size_type size_type;
@@ -326,6 +327,49 @@ struct vector_alloc_holder
holder.m_size = holder.m_capacity = 0; holder.m_size = holder.m_capacity = 0;
} }
vector_alloc_holder(pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
: Allocator(BOOST_MOVE_BASE(Allocator, holder))
, m_start(p)
, m_size(holder.m_size)
, m_capacity(capacity)
{
allocator_type &this_alloc = this->alloc();
allocator_type &x_alloc = holder.alloc();
if(allocator_traits_type::storage_can_be_propagated(x_alloc, holder.start(), this_alloc, true)){
if(this->m_capacity){
this->alloc().deallocate(this->m_start, this->m_capacity);
}
m_start = holder.m_start;
m_capacity = holder.m_capacity;
holder.m_start = pointer();
holder.m_capacity = holder.m_size = 0;
}
else if(this->m_capacity < holder.m_size){
size_type const n = holder.m_size;
pointer reuse = pointer();
m_start = this->allocation_command(allocate_new, n, m_capacity = n, reuse);
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
this->num_alloc += n != 0;
#endif
}
}
vector_alloc_holder(pointer p, size_type n)
BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible<Allocator>::value)
: Allocator()
, m_start(p)
, m_size()
, m_capacity(n)
{}
template<class AllocFwd>
vector_alloc_holder(pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
: Allocator(::boost::forward<AllocFwd>(a))
, m_start(p)
, m_size()
, m_capacity(n)
{}
~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
{ {
if(this->m_capacity){ if(this->m_capacity){
@@ -369,17 +413,17 @@ struct vector_alloc_holder
size_type m_size; size_type m_size;
size_type m_capacity; size_type m_capacity;
void swap(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
{ {
boost::adl_move_swap(this->m_start, x.m_start); boost::adl_move_swap(this->m_start, x.m_start);
boost::adl_move_swap(this->m_size, x.m_size); boost::adl_move_swap(this->m_size, x.m_size);
boost::adl_move_swap(this->m_capacity, x.m_capacity); boost::adl_move_swap(this->m_capacity, x.m_capacity);
} }
void move_from_empty(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
{ {
//this->m_size was previously initialized
this->m_start = x.m_start; this->m_start = x.m_start;
this->m_size = x.m_size;
this->m_capacity = x.m_capacity; this->m_capacity = x.m_capacity;
x.m_start = pointer(); x.m_start = pointer();
x.m_size = x.m_capacity = 0; x.m_size = x.m_capacity = 0;
@@ -417,8 +461,9 @@ struct vector_alloc_holder
(void)command; (void)command;
BOOST_ASSERT( (command & allocate_new)); BOOST_ASSERT( (command & allocate_new));
BOOST_ASSERT(!(command & nothrow_allocation)); BOOST_ASSERT(!(command & nothrow_allocation));
pointer const p = allocator_traits_type::allocate(this->alloc(), prefer_in_recvd_out_size, reuse);
reuse = pointer(); reuse = pointer();
return this->alloc().allocate(prefer_in_recvd_out_size); return p;
} }
pointer priv_allocation_command(version_2, boost::container::allocation_type command, pointer priv_allocation_command(version_2, boost::container::allocation_type command,
@@ -505,26 +550,28 @@ struct vector_alloc_holder<Allocator, version_0>
} }
} }
//Destructor void deep_swap(vector_alloc_holder &x)
~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
{}
void swap(vector_alloc_holder &x)
{ {
this->priv_swap_members_impl(x); this->priv_deep_swap(x);
} }
template<class OtherAllocator, class OtherAllocatorVersion> template<class OtherAllocator, class OtherAllocatorVersion>
void swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x) void deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
{ {
if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){ if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
throw_bad_alloc(); throw_bad_alloc();
} }
this->priv_swap_members_impl(x); this->priv_deep_swap(x);
} }
void move_from_empty(vector_alloc_holder &) void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
{ //Containers with version 0 allocators can't be moved without move elements one by one { //Containers with version 0 allocators can't be moved without moving elements one by one
throw_bad_alloc();
}
void steal_resources(vector_alloc_holder &)
{ //Containers with version 0 allocators can't be moved without moving elements one by one
throw_bad_alloc(); throw_bad_alloc();
} }
@@ -544,7 +591,7 @@ struct vector_alloc_holder<Allocator, version_0>
private: private:
template<class OtherAllocator, class OtherAllocatorVersion> template<class OtherAllocator, class OtherAllocatorVersion>
void priv_swap_members_impl(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x) void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
{ {
const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity; const size_type MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
value_type *const first_this = container_detail::to_raw_pointer(this->start()); value_type *const first_this = container_detail::to_raw_pointer(this->start());
@@ -578,7 +625,7 @@ class vector
typedef typename container_detail::version<Allocator>::type alloc_version; typedef typename container_detail::version<Allocator>::type alloc_version;
boost::container::container_detail::vector_alloc_holder boost::container::container_detail::vector_alloc_holder
<Allocator, alloc_version> m_holder; <Allocator> m_holder;
typedef allocator_traits<Allocator> allocator_traits_type; typedef allocator_traits<Allocator> allocator_traits_type;
template <class U, class UAllocator> template <class U, class UAllocator>
friend class vector; friend class vector;
@@ -587,6 +634,27 @@ class vector
typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl; typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl; typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
protected:
static bool is_propagable_from(const vector &x, const Allocator &a, bool const propagate_allocator)
{
(void)propagate_allocator;
return (allocator_traits_type::is_partially_propagable::value &&
allocator_traits_type::storage_can_be_propagated(x.get_stored_allocator(), x.m_holder.start(), a, propagate_allocator)) ||
(!allocator_traits_type::is_partially_propagable::value && allocator_traits_type::equal(a, x.get_stored_allocator()));
}
static bool are_swap_propagable(const vector &l, const vector &r, bool const propagate_allocator)
{
(void)propagate_allocator;
const allocator_type &l_a = l.get_stored_allocator();
const allocator_type &r_a = r.get_stored_allocator();
return ( allocator_traits_type::is_partially_propagable::value &&
allocator_traits_type::storage_can_be_propagated(r_a, r.m_holder.start(), l_a, propagate_allocator) &&
allocator_traits_type::storage_can_be_propagated(l_a, l.m_holder.start(), r_a, propagate_allocator)
) ||
( !allocator_traits_type::is_partially_propagable::value && allocator_traits_type::equal(l_a, r_a) );
}
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public: public:
////////////////////////////////////////////// //////////////////////////////////////////////
@@ -619,6 +687,22 @@ class vector
BOOST_COPYABLE_AND_MOVABLE(vector) BOOST_COPYABLE_AND_MOVABLE(vector)
typedef container_detail::vector_value_traits<Allocator> value_traits; typedef container_detail::vector_value_traits<Allocator> value_traits;
typedef constant_iterator<T, difference_type> cvalue_iterator; typedef constant_iterator<T, difference_type> cvalue_iterator;
protected:
void steal_resources(vector &x)
{ return this->m_holder.steal_resources(x.m_holder); }
struct initial_capacity_t{};
template<class AllocFwd>
vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
: m_holder(initial_memory, capacity, ::boost::forward<AllocFwd>(a))
{}
vector(initial_capacity_t, pointer initial_memory, size_type capacity)
: m_holder(initial_memory, capacity)
{}
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
public: public:
@@ -642,7 +726,7 @@ class vector
//! <b>Throws</b>: Nothing //! <b>Throws</b>: Nothing
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
explicit vector(const Allocator& a) BOOST_NOEXCEPT_OR_NOTHROW explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
: m_holder(a) : m_holder(a)
{} {}
@@ -761,7 +845,7 @@ class vector
template <class InIt> template <class InIt>
vector(InIt first, InIt last) vector(InIt first, InIt last)
: m_holder() : m_holder()
{ this->insert(this->cend(), first, last); } { this->assign(first, last); }
//! <b>Effects</b>: Constructs a vector that will use a copy of allocator a //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
//! and inserts a copy of the range [first, last) in the vector. //! and inserts a copy of the range [first, last) in the vector.
@@ -773,7 +857,7 @@ class vector
template <class InIt> template <class InIt>
vector(InIt first, InIt last, const allocator_type& a) vector(InIt first, InIt last, const allocator_type& a)
: m_holder(a) : m_holder(a)
{ this->insert(this->cend(), first, last); } { this->assign(first, last); }
//! <b>Effects</b>: Copy constructs a vector. //! <b>Effects</b>: Copy constructs a vector.
//! //!
@@ -796,6 +880,15 @@ class vector
, x.size(), container_detail::to_raw_pointer(this->m_holder.start())); , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
} }
//! <b>Effects</b>: Move constructor. Moves x's resources to *this.
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
: m_holder(boost::move(x.m_holder))
{ BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: Constructs a vector that will use a copy of allocator a //! <b>Effects</b>: 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 //! and inserts a copy of the range [il.begin(), il.last()) in the vector
@@ -806,19 +899,10 @@ class vector
vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type()) vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
: m_holder(a) : m_holder(a)
{ {
insert(cend(), il.begin(), il.end()); this->assign(il.begin(), il.end());
} }
#endif #endif
//! <b>Effects</b>: Move constructor. Moves x's resources to *this.
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Complexity</b>: Constant.
vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
: m_holder(boost::move(x.m_holder))
{}
#if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! <b>Effects</b>: Move constructor. Moves x's resources to *this. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
@@ -865,10 +949,10 @@ class vector
//! //!
//! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
vector(BOOST_RV_REF(vector) x, const allocator_type &a) vector(BOOST_RV_REF(vector) x, const allocator_type &a)
: m_holder(container_detail::uninitialized_size, a, x.m_holder.alloc() == a ? 0 : x.size()) : m_holder(container_detail::uninitialized_size, a, is_propagable_from(x, a, true) ? 0 : x.size())
{ {
if(x.m_holder.alloc() == a){ if(is_propagable_from(x, a, true)){
this->m_holder.move_from_empty(x.m_holder); this->m_holder.steal_resources(x.m_holder);
} }
else{ else{
const size_type n = x.size(); const size_type n = x.size();
@@ -916,7 +1000,7 @@ class vector
//! <b>Complexity</b>: Linear to the range [il.begin(), il.end()). //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
vector& operator=(std::initializer_list<value_type> il) vector& operator=(std::initializer_list<value_type> il)
{ {
assign(il.begin(), il.end()); this->assign(il.begin(), il.end());
return *this; return *this;
} }
#endif #endif
@@ -1027,7 +1111,7 @@ class vector
//! //!
void assign(std::initializer_list<T> il) void assign(std::initializer_list<T> il)
{ {
assign(il.begin(), il.end()); this->assign(il.begin(), il.end());
} }
#endif #endif
@@ -1767,7 +1851,7 @@ class vector
//! <b>Complexity</b>: Linear to the range [il.begin(), il.end()). //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
iterator insert(const_iterator position, std::initializer_list<value_type> il) iterator insert(const_iterator position, std::initializer_list<value_type> il)
{ {
return insert(position, il.begin(), il.end()); return this->insert(position, il.begin(), il.end());
} }
#endif #endif
@@ -1827,11 +1911,7 @@ class vector
|| allocator_traits_type::is_always_equal::value) && || allocator_traits_type::is_always_equal::value) &&
!container_detail::is_version<Allocator, 0>::value)) !container_detail::is_version<Allocator, 0>::value))
{ {
//Just swap internals in case of !version_0. Otherwise, deep swap this->priv_swap(x, container_detail::bool_<container_detail::is_version<Allocator, 0>::value>());
this->m_holder.swap(x.m_holder);
//And now the allocator
container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), flag);
} }
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
@@ -1849,7 +1929,7 @@ class vector
< container_detail::is_version<OtherAllocator, 0>::value && < container_detail::is_version<OtherAllocator, 0>::value &&
!container_detail::is_same<OtherAllocator, allocator_type>::value >::type * = 0 !container_detail::is_same<OtherAllocator, allocator_type>::value >::type * = 0
) )
{ this->m_holder.swap(x.m_holder); } { this->m_holder.deep_swap(x.m_holder); }
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
@@ -2035,25 +2115,32 @@ class vector
BOOST_ASSERT(this != &x); BOOST_ASSERT(this != &x);
allocator_type &this_alloc = this->m_holder.alloc(); allocator_type &this_alloc = this->m_holder.alloc();
allocator_type &x_alloc = x.m_holder.alloc(); allocator_type &x_alloc = x.m_holder.alloc();
const bool propagate_alloc = allocator_traits_type:: const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
propagate_on_container_move_assignment::value;
container_detail::bool_<propagate_alloc> flag; const bool is_propagable_from_x = is_propagable_from(x, this_alloc, propagate_alloc);
const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; const bool is_propagable_from_t = is_propagable_from(*this, x_alloc, propagate_alloc);
const bool are_both_propagable = is_propagable_from_x && is_propagable_from_t;
//Resources can be transferred if both allocators are //Resources can be transferred if both allocators are
//going to be equal after this function (either propagated or already equal) //going to be equal after this function (either propagated or already equal)
if(propagate_alloc || allocators_equal){ if(are_both_propagable){
//Destroy objects but retain memory in case x reuses it in the future //Destroy objects but retain memory in case x reuses it in the future
this->clear(); this->clear();
//Move allocator if needed this->m_holder.swap_resources(x.m_holder);
container_detail::move_alloc(this_alloc, x_alloc, flag); }
//Nothrow swap else if(is_propagable_from_x){
this->m_holder.swap(x.m_holder); this->clear();
this->m_holder.alloc().deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
this->m_holder.steal_resources(x.m_holder);
} }
//Else do a one by one move //Else do a one by one move
else{ else{
this->assign( boost::make_move_iterator(x.begin()) this->assign( boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.begin()))
, boost::make_move_iterator(x.end())); , boost::make_move_iterator(container_detail::iterator_to_raw_pointer(x.end() ))
);
} }
//Move allocator if needed
container_detail::move_alloc(this_alloc, x_alloc, container_detail::bool_<propagate_alloc>());
} }
template<class OtherAllocator> template<class OtherAllocator>
@@ -2092,6 +2179,38 @@ class vector
, container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size)); , container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size));
} }
template<class Vector> //Template it to avoid it in explicit instantiations
void priv_swap(Vector &x, container_detail::true_type) //version_0
{ this->m_holder.deep_swap(x.m_holder); }
template<class Vector> //Template it to avoid it in explicit instantiations
void priv_swap(Vector &x, container_detail::false_type) //version_N
{
const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
if(are_swap_propagable(*this, x, propagate_alloc)){
//Just swap internals
this->m_holder.swap_resources(x.m_holder);
}
else{
//Else swap element by element...
bool const t_smaller = this->size() < x.size();
vector &sml = t_smaller ? *this : x;
vector &big = t_smaller ? x : *this;
size_type const common_elements = sml.size();
for(size_type i = 0; i != common_elements; ++i){
boost::adl_move_swap(sml[i], big[i]);
}
//... and move-insert the remaining range
sml.insert( sml.cend()
, boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.nth(common_elements)))
, boost::make_move_iterator(container_detail::iterator_to_raw_pointer(big.end()))
);
}
//And now swap the allocator
container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), container_detail::bool_<propagate_alloc>());
}
void priv_reserve_no_capacity(size_type, version_0) void priv_reserve_no_capacity(size_type, version_0)
{ throw_bad_alloc(); } { throw_bad_alloc(); }
@@ -2104,7 +2223,8 @@ class vector
void priv_reserve_no_capacity(size_type new_cap, version_1) void priv_reserve_no_capacity(size_type new_cap, version_1)
{ {
//There is not enough memory, allocate a new buffer //There is not enough memory, allocate a new buffer
pointer p = this->m_holder.allocate(new_cap); //Pass the hint so that allocators can take advantage of this.
pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start);
//We will reuse insert code, so create a dummy input iterator //We will reuse insert code, so create a dummy input iterator
this->priv_forward_range_insert_new_allocation this->priv_forward_range_insert_new_allocation
( container_detail::to_raw_pointer(p), new_cap, this->back_raw(), 0, this->priv_dummy_empty_proxy()); ( container_detail::to_raw_pointer(p), new_cap, this->back_raw(), 0, this->priv_dummy_empty_proxy());
@@ -2271,7 +2391,8 @@ class vector
} }
else if(sz < cp){ else if(sz < cp){
//Allocate a new buffer. //Allocate a new buffer.
pointer p = this->m_holder.allocate(sz); //Pass the hint so that allocators can take advantage of this.
pointer const p = allocator_traits_type::allocate(this->m_holder.alloc(), sz, this->m_holder.m_start);
//We will reuse insert code, so create a dummy input iterator //We will reuse insert code, so create a dummy input iterator
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
@@ -2326,7 +2447,8 @@ class vector
T *const raw_pos = container_detail::to_raw_pointer(pos); T *const raw_pos = container_detail::to_raw_pointer(pos);
const size_type new_cap = this->m_holder.next_capacity(n); 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)); //Pass the hint so that allocators can take advantage of this.
T * const new_buf = container_detail::to_raw_pointer(allocator_traits_type::allocate(this->m_holder.alloc(), new_cap, this->m_holder.m_start));
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
++this->num_alloc; ++this->num_alloc;
#endif #endif
@@ -3007,7 +3129,7 @@ class vector
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
}; };
}} }} //namespace boost::container
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED

View File

@@ -211,7 +211,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "explicit_inst_map_test", "e
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
EndProjectSection EndProjectSection
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "explicit_inst_map_test", "explicit_inst_flat_map_test.vcproj", "{9845ECE5-C27E-4F6C-94A3-9C2E1E7231FC}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "explicit_inst_flat_map_test", "explicit_inst_flat_map_test.vcproj", "{9845ECE5-C27E-4F6C-94A3-9C2E1E7231FC}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
EndProjectSection EndProjectSection
EndProject EndProject
@@ -219,6 +219,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "explicit_inst_flat_set_test
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
EndProjectSection EndProjectSection
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "small_vector_test", "small_vector_test.vcproj", "{5CE8E110-096A-84FE-4A2A-BA7E925A6002}"
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfiguration) = preSolution GlobalSection(SolutionConfiguration) = preSolution
Debug = Debug Debug = Debug
@@ -445,6 +449,10 @@ Global
{C811D9F8-A27E-1A91-FC44-92F14F73820B}.Debug.Build.0 = Debug|Win32 {C811D9F8-A27E-1A91-FC44-92F14F73820B}.Debug.Build.0 = Debug|Win32
{C811D9F8-A27E-1A91-FC44-92F14F73820B}.Release.ActiveCfg = Release|Win32 {C811D9F8-A27E-1A91-FC44-92F14F73820B}.Release.ActiveCfg = Release|Win32
{C811D9F8-A27E-1A91-FC44-92F14F73820B}.Release.Build.0 = Release|Win32 {C811D9F8-A27E-1A91-FC44-92F14F73820B}.Release.Build.0 = Release|Win32
{5CE8E110-096A-84FE-4A2A-BA7E925A6002}.Debug.ActiveCfg = Debug|Win32
{5CE8E110-096A-84FE-4A2A-BA7E925A6002}.Debug.Build.0 = Debug|Win32
{5CE8E110-096A-84FE-4A2A-BA7E925A6002}.Release.ActiveCfg = Release|Win32
{5CE8E110-096A-84FE-4A2A-BA7E925A6002}.Release.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection EndGlobalSection

View File

@@ -146,6 +146,9 @@
<File <File
RelativePath="..\..\..\..\boost\container\slist.hpp"> RelativePath="..\..\..\..\boost\container\slist.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\small_vector.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\stable_vector.hpp"> RelativePath="..\..\..\..\boost\container\stable_vector.hpp">
</File> </File>
@@ -188,6 +191,9 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\alloc_lib.h"> RelativePath="..\..\..\..\boost\container\detail\alloc_lib.h">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\alloc_lib_auto_link.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\allocation_type.hpp"> RelativePath="..\..\..\..\boost\container\detail\allocation_type.hpp">
</File> </File>
@@ -197,9 +203,6 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\auto_link.hpp"> RelativePath="..\..\..\..\boost\container\detail\auto_link.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\boost_cont_ext_auto_link.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\compare_functors.hpp"> RelativePath="..\..\..\..\boost\container\detail\compare_functors.hpp">
</File> </File>
@@ -212,9 +215,6 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\construct_in_place.hpp"> RelativePath="..\..\..\..\boost\container\detail\construct_in_place.hpp">
</File> </File>
<File
RelativePath="..\..\test\container_common_tests.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\copy_move_algo.hpp"> RelativePath="..\..\..\..\boost\container\detail\copy_move_algo.hpp">
</File> </File>
@@ -242,6 +242,9 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\math_functions.hpp"> RelativePath="..\..\..\..\boost\container\detail\math_functions.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\min_max.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\minimal_char_traits_header.hpp"> RelativePath="..\..\..\..\boost\container\detail\minimal_char_traits_header.hpp">
</File> </File>
@@ -269,6 +272,9 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\pair.hpp"> RelativePath="..\..\..\..\boost\container\detail\pair.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\placement_new.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\pool_common.hpp"> RelativePath="..\..\..\..\boost\container\detail\pool_common.hpp">
</File> </File>
@@ -278,15 +284,9 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\singleton.hpp"> RelativePath="..\..\..\..\boost\container\detail\singleton.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\std_allocator_arg.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\std_fwd.hpp"> RelativePath="..\..\..\..\boost\container\detail\std_fwd.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\swap.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\to_raw_pointer.hpp"> RelativePath="..\..\..\..\boost\container\detail\to_raw_pointer.hpp">
</File> </File>
@@ -299,9 +299,6 @@
<File <File
RelativePath="..\..\..\..\boost\container\detail\type_traits.hpp"> RelativePath="..\..\..\..\boost\container\detail\type_traits.hpp">
</File> </File>
<File
RelativePath="..\..\..\..\boost\container\detail\utilities.hpp">
</File>
<File <File
RelativePath="..\..\..\..\boost\container\detail\value_init.hpp"> RelativePath="..\..\..\..\boost\container\detail\value_init.hpp">
</File> </File>

View File

@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="small_vector_test"
ProjectGUID="{5CE8E110-096A-84FE-4A2A-BA7E925A6002}"
Keyword="Win32Proj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../Bin/Win32/Debug"
IntermediateDirectory="Debug/small_vector_test"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../../.."
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="TRUE"
ExceptionHandling="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
TreatWChar_tAsBuiltInType="TRUE"
ForceConformanceInForLoopScope="FALSE"
UsePrecompiledHeader="0"
WarningLevel="4"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="winmm.lib"
OutputFile="$(OutDir)/small_vector_test_d.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../../../stage/lib"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="$(OutDir)/small_vector_test.pdb"
SubSystem="1"
TargetMachine="1"
FixedBaseAddress="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../Bin/Win32/Release"
IntermediateDirectory="Release/small_vector_test"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../../../.."
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
TreatWChar_tAsBuiltInType="TRUE"
ForceConformanceInForLoopScope="FALSE"
UsePrecompiledHeader="0"
WarningLevel="4"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="0"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="winmm.lib"
OutputFile="$(OutDir)/small_vector_test.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../../../stage/lib"
GenerateDebugInformation="TRUE"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
FixedBaseAddress="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4737BC0F-1242-A066-7AC5-32A2D715B4AF}">
<File
RelativePath="..\..\test\small_vector_test.cpp">
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -16,6 +16,7 @@
#include <boost/container/list.hpp> #include <boost/container/list.hpp>
#include <boost/container/slist.hpp> #include <boost/container/slist.hpp>
#include <boost/container/stable_vector.hpp> #include <boost/container/stable_vector.hpp>
#include <boost/container/small_vector.hpp>
#include <boost/container/flat_map.hpp> #include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp> #include <boost/container/flat_set.hpp>
#include <boost/container/map.hpp> #include <boost/container/map.hpp>
@@ -57,6 +58,12 @@ public:
std::allocator<Ty> m_allocator; std::allocator<Ty> m_allocator;
template <typename T> friend class SimpleAllocator; template <typename T> friend class SimpleAllocator;
friend bool operator == (const SimpleAllocator &, const SimpleAllocator &)
{ return true; }
friend bool operator != (const SimpleAllocator &, const SimpleAllocator &)
{ return false; }
}; };
class alloc_int class alloc_int
@@ -134,6 +141,7 @@ typedef deque<alloc_int, AllocIntAllocator> Deque;
typedef list<alloc_int, AllocIntAllocator> List; typedef list<alloc_int, AllocIntAllocator> List;
typedef slist<alloc_int, AllocIntAllocator> Slist; typedef slist<alloc_int, AllocIntAllocator> Slist;
typedef stable_vector<alloc_int, AllocIntAllocator> StableVector; typedef stable_vector<alloc_int, AllocIntAllocator> StableVector;
typedef small_vector<alloc_int, 9, AllocIntAllocator> SmallVector;
///////// /////////
//is_unique_assoc //is_unique_assoc
@@ -238,106 +246,97 @@ struct is_set< flat_multiset<Key, Compare, Allocator> >
//container_wrapper //container_wrapper
///////// /////////
//Try to define-allocator_aware requirements
template< class Container template< class Container
, bool Assoc = is_set<Container>::value || is_map<Container>::value , bool Assoc = is_set<Container>::value || is_map<Container>::value
, bool UniqueAssoc = is_unique_assoc<Container>::value , bool UniqueAssoc = is_unique_assoc<Container>::value
, bool Map = is_map<Container>::value , bool Map = is_map<Container>::value
> >
struct container_wrapper struct container_wrapper_inserter
: public Container
{ {
typedef typename Container::allocator_type allocator_type; typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator;
container_wrapper(const allocator_type &a) template<class Arg>
: Container(a) static iterator emplace(Container &c, const_iterator p, const Arg &arg)
{} { return c.emplace(p, arg); }
}; };
template<class Container> //map template<class Container> //map
struct container_wrapper<Container, true, true, true> struct container_wrapper_inserter<Container, true, true, true>
: public Container
{ {
typedef typename Container::allocator_type allocator_type;
typedef typename Container::key_compare key_compare;
typedef typename Container::value_type value_type;
typedef typename Container::const_iterator const_iterator; typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator; typedef typename Container::iterator iterator;
container_wrapper(const allocator_type &a)
: Container(key_compare(), a)
{}
template<class Arg> template<class Arg>
iterator emplace(const_iterator, const Arg &arg) static iterator emplace(Container &c, const_iterator, const Arg &arg)
{ { return c.emplace(arg, arg).first; }
return this->Container::emplace(arg, arg).first;
}
}; };
template<class Container> //set template<class Container> //set
struct container_wrapper<Container, true, true, false> struct container_wrapper_inserter<Container, true, true, false>
: public Container
{ {
typedef typename Container::allocator_type allocator_type;
typedef typename Container::key_compare key_compare;
typedef typename Container::value_type value_type;
typedef typename Container::const_iterator const_iterator; typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator; typedef typename Container::iterator iterator;
container_wrapper(const allocator_type &a)
: Container(key_compare(), a)
{}
template<class Arg> template<class Arg>
iterator emplace(const_iterator, const Arg &arg) static iterator emplace(Container &c, const_iterator, const Arg &arg)
{ { return c.emplace(arg).first; }
return this->Container::emplace(arg).first;
}
}; };
template<class Container> //multimap template<class Container> //multimap
struct container_wrapper<Container, true, false, true> struct container_wrapper_inserter<Container, true, false, true>
: public Container
{ {
typedef typename Container::value_type value_type;
typedef typename Container::key_compare key_compare;
typedef typename Container::allocator_type allocator_type;
typedef typename Container::const_iterator const_iterator; typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator; typedef typename Container::iterator iterator;
container_wrapper(const allocator_type &a)
: Container(key_compare(), a)
{}
template<class Arg> template<class Arg>
iterator emplace(const_iterator, const Arg &arg) static iterator emplace(Container &c, const_iterator, const Arg &arg)
{ { return c.emplace(arg, arg); }
return this->Container::emplace(arg, arg);
}
}; };
//multiset //multiset
template<class Container> //multimap template<class Container> //multimap
struct container_wrapper<Container, true, false, false> struct container_wrapper_inserter<Container, true, false, false>
{
typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator;
template<class Arg>
static iterator emplace(Container &c, const_iterator, const Arg &arg)
{ return c.emplace(arg); }
};
template< class Container>
struct container_wrapper
: public Container : public Container
{ {
typedef typename Container::value_type value_type; private:
typedef typename Container::key_compare key_compare; BOOST_COPYABLE_AND_MOVABLE(container_wrapper)
public:
typedef typename Container::allocator_type allocator_type; typedef typename Container::allocator_type allocator_type;
typedef typename Container::const_iterator const_iterator; typedef typename Container::const_iterator const_iterator;
typedef typename Container::iterator iterator; typedef typename Container::iterator iterator;
container_wrapper(const allocator_type &a) container_wrapper(const allocator_type &a)
: Container(key_compare(), a) : Container(a)
{}
container_wrapper(BOOST_RV_REF(container_wrapper) o, const allocator_type &a)
: Container(BOOST_MOVE_BASE(Container, o), a)
{}
container_wrapper(const container_wrapper &o, const allocator_type &a)
: Container(o, a)
{} {}
template<class Arg> template<class Arg>
iterator emplace(const_iterator, const Arg &arg) iterator emplace(const_iterator p, const Arg &arg)
{ { return container_wrapper_inserter<Container>::emplace(*this, p, arg); }
return this->Container::emplace(arg);
}
}; };
bool test_value_and_state_equals(const alloc_int &r, int value, int state) bool test_value_and_state_equals(const alloc_int &r, int value, int state)
{ return r.get_value() == value && r.get_allocator_state() == state; } { return r.get_value() == value && r.get_allocator_state() == state; }
@@ -354,19 +353,42 @@ bool one_level_allocator_propagation_test()
{ {
typedef container_wrapper<Container> ContainerWrapper; typedef container_wrapper<Container> ContainerWrapper;
typedef typename ContainerWrapper::iterator iterator; typedef typename ContainerWrapper::iterator iterator;
ContainerWrapper c(SimpleAllocator<MapNode>(5)); typedef typename ContainerWrapper::allocator_type allocator_type;
typedef typename ContainerWrapper::value_type value_type;
{
ContainerWrapper c(allocator_type(SimpleAllocator<value_type>(5)));
c.clear(); c.clear();
iterator it = c.emplace(c.cbegin(), 42); iterator it = c.emplace(c.cbegin(), 42);
if(!test_value_and_state_equals(*it, 42, 5)) if(!test_value_and_state_equals(*it, 42, 5))
return false; return false;
}
{
ContainerWrapper c2(allocator_type(SimpleAllocator<value_type>(4)));
ContainerWrapper c(::boost::move(c2), allocator_type(SimpleAllocator<value_type>(5)));
c.clear();
iterator it = c.emplace(c.cbegin(), 42);
if(!test_value_and_state_equals(*it, 42, 5))
return false;
}/*
{
ContainerWrapper c2(allocator_type(SimpleAllocator<value_type>(3)));
ContainerWrapper c(c2, allocator_type(SimpleAllocator<value_type>(5)));
c.clear();
iterator it = c.emplace(c.cbegin(), 42);
if(!test_value_and_state_equals(*it, 42, 5))
return false;
}*/
return true; return true;
} }
int main() int main()
{ {/*
//unique assoc //unique assoc
if(!one_level_allocator_propagation_test<FlatMap>()) if(!one_level_allocator_propagation_test<FlatMap>())
return 1; return 1;
@@ -384,7 +406,7 @@ int main()
if(!one_level_allocator_propagation_test<FlatMultiSet>()) if(!one_level_allocator_propagation_test<FlatMultiSet>())
return 1; return 1;
if(!one_level_allocator_propagation_test<MultiSet>()) if(!one_level_allocator_propagation_test<MultiSet>())
return 1; return 1;*/
//sequence containers //sequence containers
if(!one_level_allocator_propagation_test<Vector>()) if(!one_level_allocator_propagation_test<Vector>())
return 1; return 1;
@@ -396,6 +418,8 @@ int main()
return 1; return 1;
if(!one_level_allocator_propagation_test<StableVector>()) if(!one_level_allocator_propagation_test<StableVector>())
return 1; return 1;
if(!one_level_allocator_propagation_test<SmallVector>())
return 1;
return 0; return 0;
} }

151
test/small_vector_test.cpp Normal file
View File

@@ -0,0 +1,151 @@
//////////////////////////////////////////////////////////////////////////////
//
// (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.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/container/small_vector.hpp>
#include "vector_test.hpp"
#include "movable_int.hpp"
#include "propagate_allocator_test.hpp"
#include "default_init_test.hpp"
#include <boost/container/allocator.hpp>
#include <boost/container/node_allocator.hpp>
#include <boost/container/adaptive_pool.hpp>
#include <iostream>
namespace boost {
namespace container {
template class small_vector<char, 0>;
template class small_vector<char, 1>;
template class small_vector<char, 2>;
template class small_vector<char, 10>;
template class small_vector<int, 0>;
template class small_vector<int, 1>;
template class small_vector<int, 2>;
template class small_vector<int, 10>;
//Explicit instantiation to detect compilation errors
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, test::simple_allocator<test::movable_and_copyable_int> >;
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, test::dummy_test_allocator<test::movable_and_copyable_int> >;
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, std::allocator<test::movable_and_copyable_int> >;
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, allocator<test::movable_and_copyable_int> >;
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, adaptive_pool<test::movable_and_copyable_int> >;
template class boost::container::small_vector
< test::movable_and_copyable_int
, 10
, node_allocator<test::movable_and_copyable_int> >;
}}
struct boost_container_small_vector;
namespace boost { namespace container { namespace test {
template<>
struct alloc_propagate_base<boost_container_small_vector>
{
template <class T, class Allocator>
struct apply
{
typedef boost::container::small_vector<T, 10, Allocator> type;
};
};
}}} //namespace boost::container::test
int main()
{
using namespace boost::container;
/*
typedef small_vector<char, 0>::storage_test storage_test;
std::cout << "needed_extra_storages: " << storage_test::needed_extra_storages << '\n';
std::cout << "needed_bytes: " << storage_test::needed_bytes << '\n';
std::cout << "header_bytes: " << storage_test::header_bytes << '\n';
std::cout << "s_start: " << storage_test::s_start << '\n';
//char
std::cout << "sizeof(small_vector<char, 0>): " << sizeof(small_vector<char, 0>) << " extra: " << small_vector<char, 0>::needed_extra_storages << " internal storage: " << small_vector<char, 0>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<char, 1>): " << sizeof(small_vector<char, 1>) << " extra: " << small_vector<char, 1>::needed_extra_storages << " internal storage: " << small_vector<char, 1>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<char, 2>): " << sizeof(small_vector<char, 2>) << " extra: " << small_vector<char, 2>::needed_extra_storages << " internal storage: " << small_vector<char, 2>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<char, 3>): " << sizeof(small_vector<char, 3>) << " extra: " << small_vector<char, 3>::needed_extra_storages << " internal storage: " << small_vector<char, 3>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<char, 4>): " << sizeof(small_vector<char, 4>) << " extra: " << small_vector<char, 4>::needed_extra_storages << " internal storage: " << small_vector<char, 4>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<char, 5>): " << sizeof(small_vector<char, 5>) << " extra: " << small_vector<char, 5>::needed_extra_storages << " internal storage: " << small_vector<char, 5>::internal_capacity() << '\n';
std::cout << "\n";
//short
std::cout << "sizeof(small_vector<short, 0>): " << sizeof(small_vector<short, 0>) << " extra: " << small_vector<short, 0>::needed_extra_storages << " internal storage: " << small_vector<short, 0>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<short, 1>): " << sizeof(small_vector<short, 1>) << " extra: " << small_vector<short, 1>::needed_extra_storages << " internal storage: " << small_vector<short, 1>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<short, 2>): " << sizeof(small_vector<short, 2>) << " extra: " << small_vector<short, 2>::needed_extra_storages << " internal storage: " << small_vector<short, 2>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<short, 3>): " << sizeof(small_vector<short, 3>) << " extra: " << small_vector<short, 3>::needed_extra_storages << " internal storage: " << small_vector<short, 3>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<short, 4>): " << sizeof(small_vector<short, 4>) << " extra: " << small_vector<short, 4>::needed_extra_storages << " internal storage: " << small_vector<short, 4>::internal_capacity() << '\n';
std::cout << "sizeof(small_vector<short, 5>): " << sizeof(small_vector<short, 5>) << " extra: " << small_vector<short, 5>::needed_extra_storages << " internal storage: " << small_vector<short, 5>::internal_capacity() << '\n';
*/
if(test::vector_test< small_vector<int, 0> >())
return 1;
if(test::vector_test< small_vector<int, 2000> >())
return 1;
////////////////////////////////////
// Default init test
////////////////////////////////////
if(!test::default_init_test< vector<int, test::default_init_allocator<int> > >()){
std::cerr << "Default init test failed" << std::endl;
return 1;
}
////////////////////////////////////
// Emplace testing
////////////////////////////////////
const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE);
if(!boost::container::test::test_emplace< vector<test::EmplaceInt>, Options>()){
return 1;
}
////////////////////////////////////
// Allocator propagation testing
////////////////////////////////////
if(!boost::container::test::test_propagate_allocator<boost_container_small_vector>()){
return 1;
}
////////////////////////////////////
// Initializer lists testing
////////////////////////////////////
if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for<
boost::container::vector<int>
>()) {
return 1;
}
return 0;
}