priority_dequeue.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Boost.Geometry
  2. // Copyright (c) 2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP
  7. #define BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP
  8. #include <vector>
  9. #include <boost/geometry/index/detail/maxmin_heap.hpp>
  10. namespace boost { namespace geometry { namespace index { namespace detail
  11. {
  12. template
  13. <
  14. typename T,
  15. typename Container = std::vector<T>,
  16. typename Compare = std::less<typename Container::value_type>
  17. >
  18. class priority_dequeue
  19. {
  20. public:
  21. using container_type = Container;
  22. using value_compare = Compare;
  23. using value_type = typename Container::value_type;
  24. using size_type = typename Container::size_type;
  25. using reference = typename Container::reference;
  26. using const_reference = typename Container::const_reference;
  27. priority_dequeue()
  28. : c(), comp()
  29. {}
  30. explicit priority_dequeue(Compare const& compare)
  31. : c(), comp(compare)
  32. {}
  33. priority_dequeue(Compare const& compare, Container const& cont)
  34. : c(cont), comp(compare)
  35. {
  36. make_maxmin_heap(c.begin(), c.end(), comp);
  37. }
  38. priority_dequeue(Compare const& compare, Container&& cont)
  39. : c(std::move(cont)), comp(compare)
  40. {
  41. make_maxmin_heap(c.begin(), c.end(), comp);
  42. }
  43. template <typename It>
  44. priority_dequeue(It first, It last)
  45. : c(first, last), comp()
  46. {
  47. make_maxmin_heap(c.begin(), c.end(), comp);
  48. }
  49. template <typename It>
  50. priority_dequeue(It first, It last, Compare const& compare)
  51. : c(first, last), comp(compare)
  52. {
  53. make_maxmin_heap(c.begin(), c.end(), comp);
  54. }
  55. template <typename It>
  56. priority_dequeue(It first, It last, Compare const& compare, Container const& cont)
  57. : c(cont), comp(compare)
  58. {
  59. c.insert(first, last);
  60. make_maxmin_heap(c.begin(), c.end(), comp);
  61. }
  62. template <typename It>
  63. priority_dequeue(It first, It last, Compare const& compare, Container&& cont)
  64. : c(std::move(cont)), comp(compare)
  65. {
  66. c.insert(first, last);
  67. make_maxmin_heap(c.begin(), c.end(), comp);
  68. }
  69. const_reference top() const
  70. {
  71. return *c.begin();
  72. }
  73. const_reference bottom() const
  74. {
  75. return bottom_maxmin_heap(c.begin(), c.end(), comp);
  76. }
  77. void push(const value_type& value)
  78. {
  79. c.push_back(value);
  80. push_maxmin_heap(c.begin(), c.end(), comp);
  81. }
  82. void push(value_type&& value)
  83. {
  84. c.push_back(std::move(value));
  85. push_maxmin_heap(c.begin(), c.end(), comp);
  86. }
  87. template <typename ...Args>
  88. void emplace(Args&& ...args)
  89. {
  90. c.emplace_back(std::forward<Args>(args)...);
  91. push_maxmin_heap(c.begin(), c.end(), comp);
  92. }
  93. void pop_top()
  94. {
  95. pop_top_maxmin_heap(c.begin(), c.end(), comp);
  96. c.pop_back();
  97. }
  98. void pop_bottom()
  99. {
  100. pop_bottom_maxmin_heap(c.begin(), c.end(), comp);
  101. c.pop_back();
  102. }
  103. bool empty() const
  104. {
  105. return c.empty();
  106. }
  107. size_t size() const
  108. {
  109. return c.size();
  110. }
  111. void swap(priority_dequeue& other)
  112. {
  113. using std::swap;
  114. std::swap(c, other.c);
  115. std::swap(comp, other.comp);
  116. }
  117. protected:
  118. Container c;
  119. Compare comp;
  120. };
  121. }}}} // namespace boost::geometry::index::detail
  122. #endif // BOOST_GEOMETRY_INDEX_DETAIL_PRIORITY_DEQUEUE_HPP