next_capacity.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //////////////////////////////////////////////////////////////////////////////
  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. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. // container
  19. #include <boost/container/throw_exception.hpp>
  20. // container/detail
  21. #include <boost/container/detail/min_max.hpp>
  22. #include <boost/static_assert.hpp>
  23. namespace boost {
  24. namespace container {
  25. namespace dtl {
  26. template<unsigned Minimum, unsigned Numerator, unsigned Denominator>
  27. struct grow_factor_ratio
  28. {
  29. BOOST_STATIC_ASSERT(Numerator > Denominator);
  30. BOOST_STATIC_ASSERT(Numerator < 100);
  31. BOOST_STATIC_ASSERT(Denominator < 100);
  32. BOOST_STATIC_ASSERT(Denominator == 1 || (0 != Numerator % Denominator));
  33. template<class SizeType>
  34. SizeType operator()(const SizeType cur_cap, const SizeType add_min_cap, const SizeType max_cap) const
  35. {
  36. const SizeType overflow_limit = ((SizeType)-1) / Numerator;
  37. SizeType new_cap = 0;
  38. if(cur_cap <= overflow_limit){
  39. new_cap = cur_cap * Numerator / Denominator;
  40. }
  41. else if(Denominator == 1 || (SizeType(new_cap = cur_cap) / Denominator) > overflow_limit){
  42. new_cap = (SizeType)-1;
  43. }
  44. else{
  45. new_cap *= Numerator;
  46. }
  47. return max_value(SizeType(Minimum), max_value(cur_cap+add_min_cap, min_value(max_cap, new_cap)));
  48. }
  49. };
  50. } //namespace dtl {
  51. struct growth_factor_50
  52. : dtl::grow_factor_ratio<0, 3, 2>
  53. {};
  54. struct growth_factor_60
  55. : dtl::grow_factor_ratio<0, 8, 5>
  56. {};
  57. struct growth_factor_100
  58. : dtl::grow_factor_ratio<0, 2, 1>
  59. {};
  60. } //namespace container {
  61. } //namespace boost {
  62. #endif //#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP