PriorityQueue.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/PriorityQueue.h - Priority queues ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the PriorityQueue class.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_PRIORITYQUEUE_H
  19. #define LLVM_ADT_PRIORITYQUEUE_H
  20. #include <algorithm>
  21. #include <queue>
  22. namespace llvm {
  23. /// PriorityQueue - This class behaves like std::priority_queue and
  24. /// provides a few additional convenience functions.
  25. ///
  26. template<class T,
  27. class Sequence = std::vector<T>,
  28. class Compare = std::less<typename Sequence::value_type> >
  29. class PriorityQueue : public std::priority_queue<T, Sequence, Compare> {
  30. public:
  31. explicit PriorityQueue(const Compare &compare = Compare(),
  32. const Sequence &sequence = Sequence())
  33. : std::priority_queue<T, Sequence, Compare>(compare, sequence)
  34. {}
  35. template<class Iterator>
  36. PriorityQueue(Iterator begin, Iterator end,
  37. const Compare &compare = Compare(),
  38. const Sequence &sequence = Sequence())
  39. : std::priority_queue<T, Sequence, Compare>(begin, end, compare, sequence)
  40. {}
  41. /// erase_one - Erase one element from the queue, regardless of its
  42. /// position. This operation performs a linear search to find an element
  43. /// equal to t, but then uses all logarithmic-time algorithms to do
  44. /// the erase operation.
  45. ///
  46. void erase_one(const T &t) {
  47. // Linear-search to find the element.
  48. typename Sequence::size_type i = find(this->c, t) - this->c.begin();
  49. // Logarithmic-time heap bubble-up.
  50. while (i != 0) {
  51. typename Sequence::size_type parent = (i - 1) / 2;
  52. this->c[i] = this->c[parent];
  53. i = parent;
  54. }
  55. // The element we want to remove is now at the root, so we can use
  56. // priority_queue's plain pop to remove it.
  57. this->pop();
  58. }
  59. /// reheapify - If an element in the queue has changed in a way that
  60. /// affects its standing in the comparison function, the queue's
  61. /// internal state becomes invalid. Calling reheapify() resets the
  62. /// queue's state, making it valid again. This operation has time
  63. /// complexity proportional to the number of elements in the queue,
  64. /// so don't plan to use it a lot.
  65. ///
  66. void reheapify() {
  67. std::make_heap(this->c.begin(), this->c.end(), this->comp);
  68. }
  69. /// clear - Erase all elements from the queue.
  70. ///
  71. void clear() {
  72. this->c.clear();
  73. }
  74. };
  75. } // End llvm namespace
  76. #endif
  77. #ifdef __GNUC__
  78. #pragma GCC diagnostic pop
  79. #endif