-
Notifications
You must be signed in to change notification settings - Fork 3
/
PriorityQueue.h
43 lines (43 loc) · 1.1 KB
/
PriorityQueue.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
#pragma once
#include "Algorithm.h"
#include "Vector.h"
namespace ds
{
template <typename K, typename Comp = std::less<K>,typename Container=Vector<K> >
class PriorityQueue
{
public:
typedef K value_type;
typedef K *iterator;
typedef K *pointer;
typedef K &reference;
typedef const K &const_reference;
typedef const K *const_iterator;
typedef const K *const_pointer;
private:
Comp comp;
Container container;
public:
explicit PriorityQueue(Comp comp = Comp()) : comp(comp) {}
reference operator[](int x) { return container[x]; }
const_reference top() const { return container[0]; }
void pop()
{
container[0] = container[container.size() - 1];
container.pop_back();
ds::fixHeap_(container.begin(), comp, 0, container.size());
}
void push(const K &key)
{
container.push_back(key);
ds::push_heap(container.begin(), container.end(), comp);
}
void replaceFirst(const K &key)
{
container[0] = key;
ds::fixHeap_(container.begin(), comp, 0, container.size());
}
bool empty()const { return container.empty(); }
int size()const { return container.size(); }
};
}