next_capacity.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/throw_exception.hpp>
  22. // container/detail
  23. #include <boost/container/detail/min_max.hpp>
  24. namespace boost {
  25. namespace container {
  26. namespace dtl {
  27. template<unsigned Minimum, unsigned Numerator, unsigned Denominator>
  28. struct grow_factor_ratio
  29. {
  30. BOOST_CONTAINER_STATIC_ASSERT(Numerator > Denominator);
  31. BOOST_CONTAINER_STATIC_ASSERT(Numerator < 100);
  32. BOOST_CONTAINER_STATIC_ASSERT(Denominator < 100);
  33. BOOST_CONTAINER_STATIC_ASSERT(Denominator == 1 || (0 != Numerator % Denominator));
  34. template<class SizeType>
  35. SizeType operator()(const SizeType cur_cap, const SizeType add_min_cap, const SizeType max_cap) const
  36. {
  37. const SizeType overflow_limit = ((SizeType)-1) / Numerator;
  38. SizeType new_cap = 0;
  39. if(cur_cap <= overflow_limit){
  40. new_cap = SizeType(cur_cap * Numerator / Denominator);
  41. }
  42. else if(Denominator == 1 || (SizeType(new_cap = cur_cap) / Denominator) > overflow_limit){
  43. new_cap = (SizeType)-1;
  44. }
  45. else{
  46. new_cap = SizeType(new_cap*Numerator);
  47. }
  48. return max_value<SizeType>
  49. ( SizeType(Minimum)
  50. , max_value<SizeType>
  51. ( SizeType(cur_cap+add_min_cap)
  52. , min_value<SizeType>(max_cap, new_cap))
  53. );
  54. }
  55. };
  56. } //namespace dtl {
  57. struct growth_factor_50
  58. : dtl::grow_factor_ratio<0, 3, 2>
  59. {};
  60. struct growth_factor_60
  61. : dtl::grow_factor_ratio<0, 8, 5>
  62. {};
  63. struct growth_factor_100
  64. : dtl::grow_factor_ratio<0, 2, 1>
  65. {};
  66. template<class SizeType>
  67. inline void clamp_by_stored_size_type(SizeType &, SizeType)
  68. {}
  69. template<class SizeType, class SomeStoredSizeType>
  70. inline void clamp_by_stored_size_type(SizeType &s, SomeStoredSizeType)
  71. {
  72. if (s >= SomeStoredSizeType(-1) )
  73. s = SomeStoredSizeType(-1);
  74. }
  75. } //namespace container {
  76. } //namespace boost {
  77. #include <boost/container/detail/config_end.hpp>
  78. #endif //#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP