-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPQueue.h
169 lines (137 loc) · 5.29 KB
/
PQueue.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef ADS_VERTEX_PRIORITY_QUEUES_H
#define ADS_VERTEX_PRIORITY_QUEUES_H
#include "BHeap.h"
#include "Comparator.h"
#include "Defs.h"
#include "FHeap.h"
#include "LChain.h"
/**
* PQueue.h
*
* Generic interface for a priority queue of vertexes
* and its implementations.
*
* author: Giacomo Benincasa (me@gbenin.casa)
*/
namespace ADS
{
template<class KEY_TYPE, class COST_TYPE>
struct NodeDistancePair
{
NodeDistancePair (KEY_TYPE key, COST_TYPE cost);
virtual ~NodeDistancePair (void);
/* Returns true if the two objects have the same _key */
bool operator == (const NodeDistancePair &other) const;
/* Returns true if the object has higher cost of "other" */
bool operator > (const NodeDistancePair &other) const;
/* Returns true if the object has lower cost of "other" */
bool operator < (const NodeDistancePair &other) const;
KEY_TYPE _key;
COST_TYPE _cost;
};
class NodeDistancePairClass : public NodeDistancePair<key_type, cost_type>
{
public:
NodeDistancePairClass (key_type key, cost_type cost);
virtual ~NodeDistancePairClass (void);
void display (void);
};
class NDPObjPointerComparator : public ObjPointerComparator<NodeDistancePairClass>
{
public:
NDPObjPointerComparator (void);
virtual ~NDPObjPointerComparator (void);
};
class PQueue
{
public:
PQueue (void);
virtual ~PQueue (void);
/* Adds an element into the priority queue */
virtual Node<NodeDistancePairClass*> * add (NodeDistancePairClass *pNode) = 0;
/* Returns true if the priority queue is empty */
virtual bool isEmpty (void) = 0;
/* Returns the head of the priority queue.
Returns NULL if the priority queue is empty.*/
virtual NodeDistancePairClass * peek (void) = 0;
/* Returns and remove the head of the priority queue.
Returns NULL if the priority queue is empty. */
virtual NodeDistancePairClass * pop (void) = 0;
virtual NodeDistancePairClass * decreaseKey (Node<NodeDistancePairClass*> *pNode,
NodeDistancePairClass *pNewElement) = 0;
virtual void display (void) = 0;
};
/*
* Implementation of the priority queue using a binomial heap
*/
class SimplePQ : public PQueue, LChain<NodeDistancePairClass*, NDPObjPointerComparator>
{
public:
SimplePQ (bool bIsMinHeap=true);
virtual ~SimplePQ (void);
Node<NodeDistancePairClass*> * add (NodeDistancePairClass *pNode);
bool isEmpty (void);
NodeDistancePairClass * peek (void);
NodeDistancePairClass * pop (void);
NodeDistancePairClass * decreaseKey (Node<NodeDistancePairClass*> *pNode,
NodeDistancePairClass *pNewElement);
void display (void);
};
/*
* Implementation of the priority queue using a binomial heap
*/
class BHeapPQ : public PQueue, BHeap<NodeDistancePairClass*, NDPObjPointerComparator>
{
public:
BHeapPQ (bool bIsMinHeap=true);
virtual ~BHeapPQ (void);
Node<NodeDistancePairClass*> * add (NodeDistancePairClass *pNode);
bool isEmpty (void);
NodeDistancePairClass * peek (void);
NodeDistancePairClass * pop (void);
NodeDistancePairClass * decreaseKey (Node<NodeDistancePairClass*> *pNode,
NodeDistancePairClass *pNewElement);
void display (void);
};
/*
* Implementation of the priority queue using a Fibonacci heap
*/
class FHeapPQ : public PQueue, FHeap<NodeDistancePairClass*, NDPObjPointerComparator>
{
public:
FHeapPQ (bool bIsMinHeap=true);
virtual ~FHeapPQ (void);
Node<NodeDistancePairClass*> * add (NodeDistancePairClass *pNode);
bool isEmpty (void);
NodeDistancePairClass * peek (void);
NodeDistancePairClass * pop (void);
NodeDistancePairClass * decreaseKey (Node<NodeDistancePairClass*> *pNode, NodeDistancePairClass *pNewElement);
void display (void);
};
template<class KEY_TYPE, class COST_TYPE>
NodeDistancePair<KEY_TYPE, COST_TYPE>::NodeDistancePair (KEY_TYPE key, COST_TYPE cost)
{
_key = key;
_cost = cost;
}
template<class KEY_TYPE, class COST_TYPE>
NodeDistancePair<KEY_TYPE, COST_TYPE>::~NodeDistancePair()
{
}
template<class KEY_TYPE, class COST_TYPE>
bool NodeDistancePair<KEY_TYPE, COST_TYPE>::operator == (const NodeDistancePair &other) const
{
return _key == other._key;
}
template<class KEY_TYPE, class COST_TYPE>
bool NodeDistancePair<KEY_TYPE, COST_TYPE>::operator > (const NodeDistancePair &other) const
{
return _cost > other._cost;
}
template<class KEY_TYPE, class COST_TYPE>
bool NodeDistancePair<KEY_TYPE, COST_TYPE>::operator < (const NodeDistancePair &other) const
{
return _cost < other._cost;
}
}
#endif /* ADS_VERTEX_PRIORITY_QUEUES_H */