Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 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) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/container for documentation. |
| 8 | // |
| 9 | ////////////////////////////////////////////////////////////////////////////// |
| 10 | //[doc_custom_vector |
| 11 | #include <boost/container/vector.hpp> |
| 12 | #include <boost/static_assert.hpp> |
| 13 | |
| 14 | //Make sure assertions are active |
| 15 | #ifdef NDEBUG |
| 16 | #undef NDEBUG |
| 17 | #endif |
| 18 | #include <cassert> |
| 19 | |
| 20 | int main () |
| 21 | { |
| 22 | using namespace boost::container; |
| 23 | |
| 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; |
| 27 | |
| 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>) )); |
| 31 | |
| 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); |
| 38 | |
| 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; |
| 42 | |
| 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); |
| 47 | |
| 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); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | //] |