next_capacity.hpp 2.7 KB

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