WPILibC++ 2024.3.2
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 <concepts>
10#include <functional>
11#include <utility>
12#include <vector>
13
14namespace wpi {
15
16/**
17 * This class is the same as std::priority_queue with two changes:
18 *
19 * 1. Adds a remove() function for removing all elements from the priority queue
20 * that match the given value.
21 * 2. Replaces "void pop()" with "T pop()" so the element can be moved from the
22 * queue directly instead of copied from top().
23 */
24template <typename T, typename Sequence = std::vector<T>,
25 typename Compare = std::less<typename Sequence::value_type>>
26 requires std::same_as<T, typename Sequence::value_type>
28 public:
29 using value_type = typename Sequence::value_type;
30 using reference = typename Sequence::reference;
31 using const_reference = typename Sequence::const_reference;
32 using size_type = typename Sequence::size_type;
34 using value_compare = Compare;
35
36 template <typename Seq = Sequence>
37 requires std::default_initializable<Compare> &&
38 std::default_initializable<Seq>
40
41 priority_queue(const Compare& comp, const Sequence& c) : c(c), comp(comp) {
42 std::make_heap(c.begin(), c.end(), comp);
43 }
44
45 explicit priority_queue(const Compare& comp, Sequence&& c = Sequence{})
46 : c(std::move(c)), comp(comp) {
47 std::make_heap(c.begin(), c.end(), comp);
48 }
49
50 template <typename InputIterator>
51 priority_queue(InputIterator first, InputIterator last, const Compare& comp,
52 const Sequence& c)
53 : c(c), comp(comp) {
54 c.insert(c.end(), first, last);
55 std::make_heap(c.begin(), c.end(), comp);
56 }
57
58 template <typename InputIterator>
59 priority_queue(InputIterator first, InputIterator last,
60 const Compare& comp = Compare{}, Sequence&& c = Sequence{})
61 : c(std::move(c)), comp(comp) {
62 c.insert(c.end(), first, last);
63 std::make_heap(c.begin(), c.end(), comp);
64 }
65
66 [[nodiscard]]
67 bool empty() const {
68 return c.empty();
69 }
70
71 size_type size() const { return c.size(); }
72
73 const_reference top() const { return c.front(); }
74
75 void push(const value_type& value) {
76 c.push_back(value);
77 std::push_heap(c.begin(), c.end(), comp);
78 }
79
80 void push(value_type&& value) {
81 c.push_back(std::move(value));
82 std::push_heap(c.begin(), c.end(), comp);
83 }
84
85 template <typename... Args>
86 void emplace(Args&&... args) {
87 c.emplace_back(std::forward<Args>(args)...);
88 std::push_heap(c.begin(), c.end(), comp);
89 }
90
91 T pop() {
92 std::pop_heap(c.begin(), c.end(), comp);
93 auto ret = std::move(c.back());
94 c.pop_back();
95 return ret;
96 }
97
98 bool remove(const T& value) {
99 auto it = std::find(c.begin(), c.end(), value);
100 if (it != this->c.end()) {
101 c.erase(it);
102 std::make_heap(c.begin(), c.end(), comp);
103 return true;
104 } else {
105 return false;
106 }
107 }
108
109 protected:
111 Compare comp;
112};
113
114} // namespace wpi
115
116#endif // WPIUTIL_WPI_PRIORITY_QUEUE_H_
This class is the same as std::priority_queue with two changes:
Definition: priority_queue.h:27
typename Sequence::size_type size_type
Definition: priority_queue.h:32
void emplace(Args &&... args)
Definition: priority_queue.h:86
typename Sequence::value_type value_type
Definition: priority_queue.h:29
typename Sequence::const_reference const_reference
Definition: priority_queue.h:31
priority_queue(const Compare &comp, const Sequence &c)
Definition: priority_queue.h:41
bool remove(const T &value)
Definition: priority_queue.h:98
void push(const value_type &value)
Definition: priority_queue.h:75
priority_queue(InputIterator first, InputIterator last, const Compare &comp=Compare{}, Sequence &&c=Sequence{})
Definition: priority_queue.h:59
T pop()
Definition: priority_queue.h:91
bool empty() const
Definition: priority_queue.h:67
Sequence c
Definition: priority_queue.h:110
priority_queue()
Definition: priority_queue.h:39
Compare value_compare
Definition: priority_queue.h:34
Compare comp
Definition: priority_queue.h:111
void push(value_type &&value)
Definition: priority_queue.h:80
priority_queue(InputIterator first, InputIterator last, const Compare &comp, const Sequence &c)
Definition: priority_queue.h:51
const_reference top() const
Definition: priority_queue.h:73
priority_queue(const Compare &comp, Sequence &&c=Sequence{})
Definition: priority_queue.h:45
size_type size() const
Definition: priority_queue.h:71
typename Sequence::reference reference
Definition: priority_queue.h:30
Sequence container_type
Definition: priority_queue.h:33
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2120
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
CommandPtr Sequence(std::vector< CommandPtr > &&commands)
Runs a group of commands in series, one after the other.
Definition: ntcore_cpp.h:26