Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

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