forked from boostorg/container
Add experimental small_vector class
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include <boost/container/list.hpp>
|
||||
#include <boost/container/slist.hpp>
|
||||
#include <boost/container/stable_vector.hpp>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <boost/container/flat_set.hpp>
|
||||
#include <boost/container/map.hpp>
|
||||
@@ -57,6 +58,12 @@ public:
|
||||
std::allocator<Ty> m_allocator;
|
||||
|
||||
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
|
||||
@@ -134,6 +141,7 @@ typedef deque<alloc_int, AllocIntAllocator> Deque;
|
||||
typedef list<alloc_int, AllocIntAllocator> List;
|
||||
typedef slist<alloc_int, AllocIntAllocator> Slist;
|
||||
typedef stable_vector<alloc_int, AllocIntAllocator> StableVector;
|
||||
typedef small_vector<alloc_int, 9, AllocIntAllocator> SmallVector;
|
||||
|
||||
/////////
|
||||
//is_unique_assoc
|
||||
@@ -238,106 +246,97 @@ struct is_set< flat_multiset<Key, Compare, Allocator> >
|
||||
//container_wrapper
|
||||
/////////
|
||||
|
||||
//Try to define-allocator_aware requirements
|
||||
template< class Container
|
||||
, bool Assoc = is_set<Container>::value || is_map<Container>::value
|
||||
, bool UniqueAssoc = is_unique_assoc<Container>::value
|
||||
, bool Map = is_map<Container>::value
|
||||
>
|
||||
struct container_wrapper
|
||||
: public Container
|
||||
struct container_wrapper_inserter
|
||||
{
|
||||
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)
|
||||
: Container(a)
|
||||
{}
|
||||
template<class Arg>
|
||||
static iterator emplace(Container &c, const_iterator p, const Arg &arg)
|
||||
{ return c.emplace(p, arg); }
|
||||
};
|
||||
|
||||
template<class Container> //map
|
||||
struct container_wrapper<Container, true, true, true>
|
||||
: public Container
|
||||
struct container_wrapper_inserter<Container, true, true, true>
|
||||
{
|
||||
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::iterator iterator;
|
||||
|
||||
container_wrapper(const allocator_type &a)
|
||||
: Container(key_compare(), a)
|
||||
{}
|
||||
|
||||
template<class Arg>
|
||||
iterator emplace(const_iterator, const Arg &arg)
|
||||
{
|
||||
return this->Container::emplace(arg, arg).first;
|
||||
}
|
||||
static iterator emplace(Container &c, const_iterator, const Arg &arg)
|
||||
{ return c.emplace(arg, arg).first; }
|
||||
};
|
||||
|
||||
template<class Container> //set
|
||||
struct container_wrapper<Container, true, true, false>
|
||||
: public Container
|
||||
struct container_wrapper_inserter<Container, true, true, false>
|
||||
{
|
||||
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::iterator iterator;
|
||||
|
||||
container_wrapper(const allocator_type &a)
|
||||
: Container(key_compare(), a)
|
||||
{}
|
||||
|
||||
template<class Arg>
|
||||
iterator emplace(const_iterator, const Arg &arg)
|
||||
{
|
||||
return this->Container::emplace(arg).first;
|
||||
}
|
||||
static iterator emplace(Container &c, const_iterator, const Arg &arg)
|
||||
{ return c.emplace(arg).first; }
|
||||
};
|
||||
|
||||
template<class Container> //multimap
|
||||
struct container_wrapper<Container, true, false, true>
|
||||
: public Container
|
||||
struct container_wrapper_inserter<Container, true, false, true>
|
||||
{
|
||||
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::iterator iterator;
|
||||
|
||||
container_wrapper(const allocator_type &a)
|
||||
: Container(key_compare(), a)
|
||||
{}
|
||||
|
||||
template<class Arg>
|
||||
iterator emplace(const_iterator, const Arg &arg)
|
||||
{
|
||||
return this->Container::emplace(arg, arg);
|
||||
}
|
||||
static iterator emplace(Container &c, const_iterator, const Arg &arg)
|
||||
{ return c.emplace(arg, arg); }
|
||||
};
|
||||
|
||||
//multiset
|
||||
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
|
||||
{
|
||||
typedef typename Container::value_type value_type;
|
||||
typedef typename Container::key_compare key_compare;
|
||||
private:
|
||||
BOOST_COPYABLE_AND_MOVABLE(container_wrapper)
|
||||
|
||||
public:
|
||||
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)
|
||||
: 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>
|
||||
iterator emplace(const_iterator, const Arg &arg)
|
||||
{
|
||||
return this->Container::emplace(arg);
|
||||
}
|
||||
iterator emplace(const_iterator p, const Arg &arg)
|
||||
{ return container_wrapper_inserter<Container>::emplace(*this, p, arg); }
|
||||
};
|
||||
|
||||
|
||||
bool test_value_and_state_equals(const alloc_int &r, int value, int 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 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();
|
||||
iterator it = c.emplace(c.cbegin(), 42);
|
||||
c.clear();
|
||||
iterator it = c.emplace(c.cbegin(), 42);
|
||||
|
||||
if(!test_value_and_state_equals(*it, 42, 5))
|
||||
return false;
|
||||
if(!test_value_and_state_equals(*it, 42, 5))
|
||||
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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{/*
|
||||
//unique assoc
|
||||
if(!one_level_allocator_propagation_test<FlatMap>())
|
||||
return 1;
|
||||
@@ -384,7 +406,7 @@ int main()
|
||||
if(!one_level_allocator_propagation_test<FlatMultiSet>())
|
||||
return 1;
|
||||
if(!one_level_allocator_propagation_test<MultiSet>())
|
||||
return 1;
|
||||
return 1;*/
|
||||
//sequence containers
|
||||
if(!one_level_allocator_propagation_test<Vector>())
|
||||
return 1;
|
||||
@@ -396,6 +418,8 @@ int main()
|
||||
return 1;
|
||||
if(!one_level_allocator_propagation_test<StableVector>())
|
||||
return 1;
|
||||
if(!one_level_allocator_propagation_test<SmallVector>())
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user