forked from 1flei/obstacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_pri_queue.h
104 lines (89 loc) · 2.42 KB
/
my_pri_queue.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <set>
#include <unordered_map>
#include "subtidx.h"
//priority queue used for subtidx
class SubtPriQueue
{
using KeyType = std::pair<double, SubtIdx>;
std::set<KeyType> key_set;
std::unordered_map<int, std::set<KeyType>::iterator > iter_map;
public:
void emplace(double dist, SubtIdx st) {
int tid = st.tid;
auto it = iter_map.find(tid);
if(it != iter_map.end()){
//tid is already in queue
if(dist < it->second->first){
key_set.erase(it->second);
iter_map[tid] = key_set.emplace(dist, st).first;
}
} else{
iter_map[tid] = key_set.emplace(dist, st).first;
}
}
void emplace_k(double dist, SubtIdx st, int k) {
if(size() < k) {
emplace(dist, st);
} else{
auto& maxk = maximum();
if(dist < maxk.first){
//push, will either update the current key or push a new key
emplace(dist, st);
if(size() > k){
//if pushed a new key
pop_maximum();
}
}
}
}
size_t size(){
return key_set.size();
}
const KeyType& top() {
return *(key_set.begin());
}
const KeyType& minimum() {
return *(key_set.begin());
}
const KeyType& maximum() {
return *(key_set.rbegin());
}
void pop() {
auto kt = top();
iter_map.erase(kt.second.tid);
key_set.erase(key_set.begin());
}
void pop_minimum() {
pop();
}
void pop_maximum() {
auto kt = maximum();
iter_map.erase(kt.second.tid);
key_set.erase(std::prev(key_set.end()));
}
std::set<KeyType>::iterator begin() {
return key_set.begin();
}
std::set<KeyType>::iterator end() {
return key_set.end();
}
const std::set<KeyType>::iterator begin() const {
return key_set.begin();
}
const std::set<KeyType>::iterator end() const {
return key_set.end();
}
bool empty() const {
return key_set.empty();
}
void erase(const KeyType& kt){
key_set.erase(kt);
iter_map.erase(kt.second.tid);
}
std::set<KeyType>::iterator erase(std::set<KeyType>::iterator it){
auto ret = key_set.erase(it);
iter_map.erase(it->second.tid);
return ret;
}
};