]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/example/doc_custom_vector.cpp
1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2013-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/container/vector.hpp>
12 #include <boost/static_assert.hpp>
14 //Make sure assertions are active
22 using namespace boost::container
;
24 //This option specifies that a vector that will use "unsigned char" as
25 //the type to store capacity or size internally.
26 typedef vector_options
< stored_size
<unsigned char> >::type size_option_t
;
28 //Size-optimized vector is smaller than the default one.
29 typedef vector
<int, new_allocator
<int>, size_option_t
> size_optimized_vector_t
;
30 BOOST_STATIC_ASSERT(( sizeof(size_optimized_vector_t
) < sizeof(vector
<int>) ));
32 //Requesting capacity for more elements than representable by "unsigned char"
33 //is an error in the size optimized vector.
34 bool exception_thrown
= false;
35 try { size_optimized_vector_t
v(256); }
36 catch(...){ exception_thrown
= true; }
37 assert(exception_thrown
== true);
39 //This option specifies that a vector will increase its capacity 50%
40 //each time the previous capacity was exhausted.
41 typedef vector_options
< growth_factor
<growth_factor_50
> >::type growth_50_option_t
;
43 //Fill the vector until full capacity is reached
44 vector
<int, new_allocator
<int>, growth_50_option_t
> growth_50_vector(5, 0);
45 const std::size_t old_cap
= growth_50_vector
.capacity();
46 growth_50_vector
.resize(old_cap
);
48 //Now insert an additional item and check the new buffer is 50% bigger
49 growth_50_vector
.push_back(1);
50 assert(growth_50_vector
.capacity() == old_cap
*3/2);