-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpriority_queue.h
43 lines (33 loc) · 1015 Bytes
/
priority_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
#ifndef C_SRC_PRIORITY_QUEUE_H_
#define C_SRC_PRIORITY_QUEUE_H_
#include "macros.h"
typedef bool(*LessFun)(void*, void*);
typedef void(*UpdatePositionFun)(void*, int);
typedef void(*DestroyElementFun)(void*);
class PriorityQueue
{
public:
PriorityQueue(LessFun ls, UpdatePositionFun upd, DestroyElementFun dtor);
~PriorityQueue();
bool insert(void* item);
bool remove(void* item, int pos);
void* remove(int pos);
void* pop() {return remove(0);}
void* peek();
int size() const { return length_;}
int rank(int pos) {return rank_r(pos, 0)+1;}
private:
inline void set(int pos, void* item);
inline void pos_swap(int pos1, int pos2);
void bubble_down(int pos);
void bubble_up(int pos);
int rank_r(int x, int cur_node_index);
int capacity_;
int length_;
void** heap_;
LessFun less_;
UpdatePositionFun update_pos_fun_;
DestroyElementFun item_dtor_;
DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
};
#endif // C_SRC_PRIORITY_QUEUE_H_