WPILibC++ 2023.4.3
priority_queue.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPIUTIL_WPI_PRIORITY_QUEUE_H_
6#define WPIUTIL_WPI_PRIORITY_QUEUE_H_
7
8#include <algorithm>
9#include <functional>
10#include <utility>
11#include <vector>
12
13namespace wpi {
14
15/**
16 * This class is the same as std::priority_queue with two changes:
17 *
18 * 1. Adds a remove() function for removing all elements from the priority queue
19 * that match the given value.
20 * 2. Replaces "void pop()" with "T pop()" so the element can be moved from the
21 * queue directly instead of copied from top().
22 */
23template <typename T, typename Sequence = std::vector<T>,
24 typename Compare = std::less<typename Sequence::value_type>>
26 public:
27 static_assert(std::is_same_v<T, typename Sequence::value_type>,
28 "value_type must be the same as the underlying container");
29
30 using value_type = typename Sequence::value_type;
31 using reference = typename Sequence::reference;
32 using const_reference = typename Sequence::const_reference;
33 using size_type = typename Sequence::size_type;
35 using value_compare = Compare;
36
37 template <typename Seq = Sequence,
38 typename Requires = typename std::enable_if_t<
39 std::is_default_constructible<Compare>{} &&
40 std::is_default_constructible<Seq>{}>>
42
43 priority_queue(const Compare& comp, const Sequence& c) : c(c), comp(comp) {
44 std::make_heap(c.begin(), c.end(), comp);
45 }
46
47 explicit priority_queue(const Compare& comp, Sequence&& c = Sequence{})
48 : c(std::move(c)), comp(comp) {
49 std::make_heap(c.begin(), c.end(), comp);
50 }
51
52 template <typename InputIterator>
53 priority_queue(InputIterator first, InputIterator last, const Compare& comp,
54 const Sequence& c)
55 : c(c), comp(comp) {
56 c.insert(c.end(), first, last);
57 std::make_heap(c.begin(), c.end(), comp);
58 }
59
60 template <typename InputIterator>
61 priority_queue(InputIterator first, InputIterator last,
62 const Compare& comp = Compare{}, Sequence&& c = Sequence{})
63 : c(std::move(c)), comp(comp) {
64 c.insert(c.end(), first, last);
65 std::make_heap(c.begin(), c.end(), comp);
66 }
67
68 [[nodiscard]] bool empty() const { return c.empty(); }
69
70 size_type size() const { return c.size(); }
71
72 const_reference top() const { return c.front(); }
73
74 void push(const value_type& value) {
75 c.push_back(value);
76 std::push_heap(c.begin(), c.end(), comp);
77 }
78
80 c.push_back(std::move(value));
81 std::push_heap(c.begin(), c.end(), comp);
82 }
83
84 template <typename... Args>
85 void emplace(Args&&... args) {
86 c.emplace_back(std::forward<Args>(args)...);
87 std::push_heap(c.begin(), c.end(), comp);
88 }
89
90 T pop() {
91 std::pop_heap(c.begin(), c.end(), comp);
92 auto ret = std::move(c.back());
93 c.pop_back();
94 return ret;
95 }
96
97 bool remove(const T& value) {
98 auto it = std::find(c.begin(), c.end(), value);
99 if (it != this->c.end()) {
100 c.erase(it);
101 std::make_heap(c.begin(), c.end(), comp);
102 return true;
103 } else {
104 return false;
105 }
106 }
107
108 protected:
110 Compare comp;
111};
112
113} // namespace wpi
114
115#endif // WPIUTIL_WPI_PRIORITY_QUEUE_H_
Definition: core.h:1240
This class is the same as std::priority_queue with two changes:
Definition: priority_queue.h:25
typename Sequence::size_type size_type
Definition: priority_queue.h:33
void emplace(Args &&... args)
Definition: priority_queue.h:85
typename Sequence::value_type value_type
Definition: priority_queue.h:30
typename Sequence::const_reference const_reference
Definition: priority_queue.h:32
priority_queue(const Compare &comp, const Sequence &c)
Definition: priority_queue.h:43
bool remove(const T &value)
Definition: priority_queue.h:97
void push(const value_type &value)
Definition: priority_queue.h:74
priority_queue(InputIterator first, InputIterator last, const Compare &comp=Compare{}, Sequence &&c=Sequence{})
Definition: priority_queue.h:61
T pop()
Definition: priority_queue.h:90
priority_queue()
Definition: priority_queue.h:41
bool empty() const
Definition: priority_queue.h:68
Sequence c
Definition: priority_queue.h:109
Compare value_compare
Definition: priority_queue.h:35
Compare comp
Definition: priority_queue.h:110
void push(value_type &&value)
Definition: priority_queue.h:79
priority_queue(InputIterator first, InputIterator last, const Compare &comp, const Sequence &c)
Definition: priority_queue.h:53
const_reference top() const
Definition: priority_queue.h:72
priority_queue(const Compare &comp, Sequence &&c=Sequence{})
Definition: priority_queue.h:47
size_type size() const
Definition: priority_queue.h:70
typename Sequence::reference reference
Definition: priority_queue.h:31
Sequence container_type
Definition: priority_queue.h:34
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2318
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
Can be used as a parameter to Eigen::seq and Eigen::seqN functions to symbolically reference the last...
Definition: IndexedViewHelper.h:38
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
Definition: IndexedViewHelper.h:81
CommandPtr Sequence(std::vector< CommandPtr > &&commands)
Runs a group of commands in series, one after the other.
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18