-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenlist.h
More file actions
101 lines (87 loc) · 3.16 KB
/
openlist.h
File metadata and controls
101 lines (87 loc) · 3.16 KB
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
#ifndef OPENLIST_H
#define OPENLIST_H
#include "node.h"
#include "gl_const.h"
#include <list>
#include <vector>
//representation of OPEN list as vector of lists, which provides both fast access to the element with minimum key value and fast search by the cell value
class OpenList {
public:
~OpenList() {
for (auto elem : elements) {
if(!elem.empty()) elem.erase(elem.begin(), elem.end());
}
}
inline bool empty() const {
for (auto elem : elements) {
if(!elem.empty()) return false;
}
return true;
}
inline Node* get() { //return node wit minimum key value
Node* best = elements[current_top_coord].front();
if (best->g > best->rhs) elements[current_top_coord].pop_front();
return best;
}
inline bool top_key_less_than(Key cur_key) { //compare the minimum key value and current key value
bool exists = false;
Key best_key(std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
for (size_t i = 0; i < elements.size(); i++) {
if (!elements[i].empty()) {
exists = true;
if (elements[i].front()->key < best_key) {
best_key = elements[i].front()->key;
current_top_coord = i;
}
}
}
if(!exists) return false;
return best_key < cur_key;
}
inline void put (Node* item) { //add node to OPEN list or renew it's key value, is it is already there
if (elements[item->cell.y].empty()) {
elements[item->cell.y].emplace_back(item);
return;
}
elements[item->cell.y].remove_if([item](Node* curr) { return curr->cell == item->cell; });
std::list<Node*>::iterator it = elements[item->cell.y].begin();
std::list<Node*>::iterator position = elements[item->cell.y].end();
while (it != elements[item->cell.y].end()) {
if ((*it)->key < item->key) {
position = it;
}
++it;
}
if (++position != elements[item->cell.y].end()) elements[item->cell.y].emplace(position, item);
else elements[item->cell.y].emplace_back(item);
}
inline void remove_if(Node* item) {
elements[item->cell.y].remove_if([item](Node* curr) { return curr->cell == item->cell; });
}
inline void resize(int value) {
elements.resize(value);
}
std::vector<Node> get_elements() const {
std::vector<Node> result;
for (auto elem : elements) {
for(auto it = elem.begin(); it != elem.end(); ++it) {
result.push_back(*(*it));
}
}
return result;
}
void print_elements() const {
for (auto elem : elements) {
if (!elem.empty()) {
for(auto it = elem.begin(); it != elem.end(); ++it) {
std::cout << (*it)->cell << (*it)->key.k1 << " ";
}
std::cout << std::endl;
} else std::cout << "None\n";
}
}
private:
std::vector<std::list<Node*> > elements;
int current_top_coord;
};
#endif // OPENLIST_H