Skip to content

Commit 9960bc3

Browse files
author
vadim-xd
committed
Add TPriorityQueue::PopValue
ce37597283b1508bdba021056c149fe16feb1c69
1 parent e03d614 commit 9960bc3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

util/generic/queue.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class TPriorityQueue: public std::priority_queue<T, S, C> {
4848
this->c.clear();
4949
}
5050

51+
inline T PopValue() {
52+
Y_ASSERT(!this->empty());
53+
std::pop_heap(Container().begin(), Container().end(), this->comp);
54+
T value = std::move(Container().back());
55+
this->c.pop_back();
56+
return value;
57+
}
58+
5159
inline S& Container() Y_LIFETIME_BOUND {
5260
return this->c;
5361
}

util/generic/queue_ut.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "queue.h"
2+
#include "deque.h"
23
#include "list.h"
3-
#include "vector.h"
4+
#include "ptr.h"
45

56
#include <library/cpp/testing/unittest/registar.h>
67

@@ -152,6 +153,41 @@ Y_UNIT_TEST_SUITE(TYQueueTest) {
152153
UNIT_ASSERT(q.empty());
153154
}
154155

156+
struct THolderWithPriority {
157+
THolderWithPriority(const TString& value, int priority)
158+
: Value(MakeHolder<TString>(value))
159+
, Priority(priority)
160+
{
161+
}
162+
163+
THolder<TString> Value; // THolder to test move-ctor
164+
int Priority;
165+
166+
std::weak_ordering operator<=>(const THolderWithPriority& other) const noexcept {
167+
return Priority <=> other.Priority;
168+
}
169+
};
170+
171+
Y_UNIT_TEST(pqueue5) {
172+
// Test move-and-pop
173+
TPriorityQueue<THolderWithPriority> q;
174+
175+
UNIT_ASSERT(q.empty());
176+
q.emplace("min", 1);
177+
q.emplace("max", 3);
178+
q.emplace("middle", 2);
179+
180+
auto value = q.PopValue().Value;
181+
UNIT_ASSERT(*value == "max");
182+
183+
value = q.PopValue().Value;
184+
UNIT_ASSERT(*value == "middle");
185+
186+
value = q.PopValue().Value;
187+
UNIT_ASSERT(*value == "min");
188+
UNIT_ASSERT(q.empty());
189+
}
190+
155191
Y_UNIT_TEST(queue1) {
156192
TQueue<int, TList<int>> q;
157193

0 commit comments

Comments
 (0)