next_capacity.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace boost {
  23. namespace container {
  24. namespace container_detail {
  25. enum NextCapacityOption { NextCapacityDouble, NextCapacity60Percent };
  26. template<class SizeType, NextCapacityOption Option>
  27. struct next_capacity_calculator;
  28. template<class SizeType>
  29. struct next_capacity_calculator<SizeType, NextCapacityDouble>
  30. {
  31. static SizeType get(const SizeType max_size
  32. ,const SizeType capacity
  33. ,const SizeType n)
  34. {
  35. const SizeType remaining = max_size - capacity;
  36. if ( remaining < n )
  37. boost::container::throw_length_error("get_next_capacity, allocator's max_size reached");
  38. const SizeType additional = max_value(n, capacity);
  39. return ( remaining < additional ) ? max_size : ( capacity + additional );
  40. }
  41. };
  42. template<class SizeType>
  43. struct next_capacity_calculator<SizeType, NextCapacity60Percent>
  44. {
  45. static SizeType get(const SizeType max_size
  46. ,const SizeType capacity
  47. ,const SizeType n)
  48. {
  49. const SizeType remaining = max_size - capacity;
  50. if ( remaining < n )
  51. boost::container::throw_length_error("get_next_capacity, allocator's max_size reached");
  52. const SizeType m3 = max_size/3;
  53. if (capacity < m3)
  54. return capacity + max_value(3*(capacity+1)/5, n);
  55. if (capacity < m3*2)
  56. return capacity + max_value((capacity+1)/2, n);
  57. return max_size;
  58. }
  59. };
  60. } //namespace container_detail {
  61. } //namespace container {
  62. } //namespace boost {
  63. #endif //#ifndef BOOST_CONTAINER_DETAIL_NEXT_CAPACITY_HPP