ext-boost/boost/container/detail/next_capacity.hpp

78 lines
2.1 KiB
C++
Raw Normal View History

2015-08-16 23:45:50 +00:00
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2014-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_DETAIL_NEXT_CAPACITY_HPP
#define BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
// container
#include <boost/container/throw_exception.hpp>
// container/detail
#include <boost/container/detail/min_max.hpp>
#include <boost/static_assert.hpp>
2015-08-16 23:45:50 +00:00
namespace boost {
namespace container {
namespace dtl {
2015-08-16 23:45:50 +00:00
template<unsigned Minimum, unsigned Numerator, unsigned Denominator>
struct grow_factor_ratio
2015-08-16 23:45:50 +00:00
{
BOOST_STATIC_ASSERT(Numerator > Denominator);
BOOST_STATIC_ASSERT(Numerator < 100);
BOOST_STATIC_ASSERT(Denominator < 100);
BOOST_STATIC_ASSERT(Denominator == 1 || (0 != Numerator % Denominator));
2015-08-16 23:45:50 +00:00
template<class SizeType>
SizeType operator()(const SizeType cur_cap, const SizeType add_min_cap, const SizeType max_cap) const
2015-08-16 23:45:50 +00:00
{
const SizeType overflow_limit = ((SizeType)-1) / Numerator;
2015-08-16 23:45:50 +00:00
SizeType new_cap = 0;
2015-08-16 23:45:50 +00:00
if(cur_cap <= overflow_limit){
new_cap = cur_cap * Numerator / Denominator;
}
else if(Denominator == 1 || (SizeType(new_cap = cur_cap) / Denominator) > overflow_limit){
new_cap = (SizeType)-1;
}
else{
new_cap *= Numerator;
}
return max_value(SizeType(Minimum), max_value(cur_cap+add_min_cap, min_value(max_cap, new_cap)));
2015-08-16 23:45:50 +00:00
}
};
} //namespace dtl {
struct growth_factor_50
: dtl::grow_factor_ratio<0, 3, 2>
{};
struct growth_factor_60
: dtl::grow_factor_ratio<0, 8, 5>
{};
struct growth_factor_100
: dtl::grow_factor_ratio<0, 2, 1>
{};
2015-08-16 23:45:50 +00:00
} //namespace container {
} //namespace boost {
#endif //#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP