-
Notifications
You must be signed in to change notification settings - Fork 0
/
PQueueSearcher.h
104 lines (84 loc) · 2.2 KB
/
PQueueSearcher.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
//
// Created by tomer on 1/12/19.
//
#ifndef FLIGTSIMPROJ2_PQUEUESEARCHER_H
#define FLIGTSIMPROJ2_PQUEUESEARCHER_H
#include "ISearcher.h"
#include "State.h"
#include "Searcher.h"
#include "StatePriority.h"
#include "CompareState.h"
#include "ISearchable.h"
#include <queue>
template <class T>
//for Best First Search
class PQueueSearcher {
private:
priority_queue<StatePriority<T>*,
vector<StatePriority<T>*>,
PriorityStateCompare<T>> p_queue;
public:
State<T>* popOpenList();
void clearAllStates();
void addToOpenList(State<T>* state);
bool isOpenEmpty();
virtual double getStatePriority(State<T>* state)=0;
};
template<class T>
bool PQueueSearcher<T>::isOpenEmpty() {
if(p_queue.empty())
return true;
return false;
}
template<class T>
void PQueueSearcher<T>::clearAllStates() {
Searcher<T> ::clearAllStates();
while (!p_queue.empty()){
StatePriority<T>* state_p = p_queue.top();
p_queue.pop();
if(!state_p->getState()->appearInSolution()){
delete state_p;
}
}
}
template<class T>
State<T> *PQueueSearcher<T>::popOpenList() {
Searcher<T> :: nodesEvaluated++;
StatePriority<T>* state_p= p_queue.top();
p_queue.top();
State<T>* save_state = state_p->getState();
delete(state_p);
return save_state;
}
template<class T>
void PQueueSearcher<T>::addToOpenList(State<T>* state) {
vector<StatePriority<T>*> vector1;
bool shouldBePushed = true;
StatePriority<T>* state_p = new StatePriority<T>(state, getStatePriority(state));
while(!p_queue.empty()){
StatePriority<T>* curr = p_queue.top();
p_queue.pop();
if(*curr->getState()==*state){
if(curr->getPriority()<state_p->getPriority()){
shouldBePushed= false;
}
else{
delete curr->getState();
delete curr;
}
break;
}
vector1.push_back(curr);
}
if(shouldBePushed){
p_queue.push(state_p);
}
else{
delete state;
delete state_p;
}
for(auto y : vector1){
p_queue.push(y);
}
}
#endif //FLIGTSIMPROJ2_PQUEUESEARCHER_H