-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.tpp
More file actions
70 lines (60 loc) · 1.59 KB
/
queue.tpp
File metadata and controls
70 lines (60 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef QUEUE_TPP
#define QUEUE_TPP
#include <algorithm>
#include <stack>
#include <utility>
#include "queue.h"
template <class T>
void Queue<T>::Push(T&& elem) {
T push_stack_min{push_stack_.empty() ? elem : std::min(push_stack_.top().second, elem)};
push_stack_.emplace(std::forward<T>(elem), std::move(push_stack_min));
if (push_stack_.size() == 1) {
push_stack_end_ = &(push_stack_.top());
}
}
template <class T>
T Queue<T>::Pop() {
if (pop_stack_.empty()) {
T pop_stack_min{push_stack_.top().first};
pop_stack_.emplace(std::move(push_stack_.top().first), std::move(pop_stack_min));
push_stack_.pop();
pop_stack_end_ = &(pop_stack_.top());
while (!push_stack_.empty()) {
pop_stack_min = std::min(pop_stack_.top().second, push_stack_.top().first);
pop_stack_.emplace(std::move(push_stack_.top().first), std::move(pop_stack_min));
push_stack_.pop();
}
push_stack_end_ = nullptr;
}
T tmp{pop_stack_.top().first};
pop_stack_.pop();
if (pop_stack_.empty()) {
pop_stack_end_ = nullptr;
}
return tmp;
}
template <class T>
T& Queue<T>::Front() {
if (!pop_stack_.empty()) {
return pop_stack_.top().first;
}
return push_stack_end_->first;
}
template <class T>
T& Queue<T>::Back() {
if (!push_stack_.empty()) {
return push_stack_.top().first;
}
return pop_stack_end_->first;
}
template <class T>
T Queue<T>::Min() {
if (push_stack_.empty()) {
return pop_stack_.top().second;
}
if (pop_stack_.empty()) {
return push_stack_.top().second;
}
return std::min(push_stack_.top().second, pop_stack_.top().second);
}
#endif