blob: 7e6554ded063869af884f5bfc1b98e5be96729d6 [file] [log] [blame]
Brian Silvermanfad8f552018-08-04 23:36:19 -07001//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2014-2015. 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#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP
11#define BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP
12
13#ifndef BOOST_CONFIG_HPP
14# include <boost/config.hpp>
15#endif
16
17#if defined(BOOST_HAS_PRAGMA_ONCE)
18# pragma once
19#endif
20
21// container
22#include <boost/container/throw_exception.hpp>
23// container/detail
24#include <boost/container/detail/min_max.hpp>
25
26#include <boost/static_assert.hpp>
27
28namespace boost {
29namespace container {
30namespace dtl {
31
32template<unsigned Minimum, unsigned Numerator, unsigned Denominator>
33struct grow_factor_ratio
34{
35 BOOST_STATIC_ASSERT(Numerator > Denominator);
36 BOOST_STATIC_ASSERT(Numerator < 100);
37 BOOST_STATIC_ASSERT(Denominator < 100);
38 BOOST_STATIC_ASSERT(Denominator == 1 || (0 != Numerator % Denominator));
39
40 template<class SizeType>
41 SizeType operator()(const SizeType cur_cap, const SizeType add_min_cap, const SizeType max_cap) const
42 {
43 const SizeType overflow_limit = ((SizeType)-1) / Numerator;
44
45 SizeType new_cap = 0;
46
47 if(cur_cap <= overflow_limit){
48 new_cap = cur_cap * Numerator / Denominator;
49 }
50 else if(Denominator == 1 || (SizeType(new_cap = cur_cap) / Denominator) > overflow_limit){
51 new_cap = (SizeType)-1;
52 }
53 else{
54 new_cap *= Numerator;
55 }
56 return max_value(SizeType(Minimum), max_value(cur_cap+add_min_cap, min_value(max_cap, new_cap)));
57 }
58};
59
60} //namespace dtl {
61
62struct growth_factor_50
63 : dtl::grow_factor_ratio<0, 3, 2>
64{};
65
66struct growth_factor_60
67 : dtl::grow_factor_ratio<0, 8, 5>
68{};
69
70struct growth_factor_100
71 : dtl::grow_factor_ratio<0, 2, 1>
72{};
73
74} //namespace container {
75} //namespace boost {
76
77#endif //#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP