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