-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenlist.cpp
More file actions
149 lines (131 loc) · 4.53 KB
/
openlist.cpp
File metadata and controls
149 lines (131 loc) · 4.53 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
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
#include "openlist.h"
OpenList::OpenList() { size = 0; }
OpenList::OpenList(int size_) {
elements.resize(size_);
size = 0;
height = size_;
}
void OpenList::resize(int size_) {
elements.resize(size_);
height = size_;
size = 0;
}
OpenList::~OpenList() {
for (auto elem : elements) {
if(!elem.empty()) elem.erase(elem.begin(), elem.end());
}
}
size_t OpenList::get_size() const {
return size;
}
bool OpenList::is_empty() const {
if (size == 0) return true;
return false;
}
bool OpenList::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 < height; i++) {
if (!elements[i].empty()) {
exists = true;
if (elements[i].front()->key < best_key) {
best_key = elements[i].front()->key;
top_coord = i;
}
}
}
if(!exists) return false;
return best_key < cur_key;
}
Node* OpenList::get() { //return node wit minimum key value
Node* best = elements[top_coord].front();
elements[top_coord].pop_front();
return best;
}
void OpenList::clear() {
elements.erase(elements.begin(), elements.end());
}
void OpenList::print_elements() const {
for (auto elem : elements) {
if (!elem.empty()) {
for(auto it = elem.begin(); it != elem.end(); ++it) {
std::cout << (**it) << "<-" << *(*it)->parent << (*it)->key.k1 << ' ' << (*it)->key.k2 << " ";
}
std::cout << std::endl;
}
}
std::cout << "end\n";
}
void OpenList::put (Node* item) { //add node to OPEN list or renew it's key value, is it is already there
if (elements[item->i].empty()) {
elements[item->i].push_back(item);
++size;
return;
}
std::list<Node*>::iterator pos = elements[item->i].end();
bool pos_found = false;
for(auto it = elements[item->i].begin(); it != elements[item->i].end(); ++it) {
if (item->key < (*it)->key && (!pos_found)) {
pos = it;
pos_found = true;
}
if (**it == *item) {
if (!(item->key < (*it)->key)) return;
else {
if(pos == it) {
*it = item;
return;
}
elements[item->i].erase(it);
--size;
break;
}
}
}
++size;
elements[item->i].insert(pos, item);
}
void OpenList::remove_if(Node* item) {
elements[item->i].remove_if([item](Node* curr) { return *curr == *item; });
}
bool OpenList::find(Node* item) {
return std::find_if(elements[item->i].begin(), elements[item->i].end(),[item](Node* curr) { return *curr == *item; }) != elements[item->i].end();
}
void OpenList::remove_all(Node item) {
elements[item.i].remove_if([item](Node* curr) { return curr->i == item.i && curr->j == item.j; });
}
tinyxml2::XMLElement * OpenList::writeToXml(tinyxml2::XMLElement * element, tinyxml2::XMLNode * child) const {
Node min;
for(size_t i = 0; i < height; i++) {
if(!elements[i].empty() && elements[i].front()->key < min.key) {
min = *elements[i].front();
}
}
if(min.g != std::numeric_limits<float>::infinity()) {
element->SetAttribute(CNS_TAG_ATTR_X, min.j);
element->SetAttribute(CNS_TAG_ATTR_Y, min.i);
element->SetAttribute(CNS_TAG_ATTR_F, min.rhs);
element->SetAttribute(CNS_TAG_ATTR_G, min.g);
element->SetAttribute(CNS_TAG_ATTR_PARX, min.parent->j);
element->SetAttribute(CNS_TAG_ATTR_PARY, min.parent->i);
child->InsertEndChild(element);
}
for(size_t i = 0; i < height; ++i) {
if(!elements[i].empty()) {
for (auto it = elements[i].begin(); it != elements[i].end(); ++it) {
if (**it != min) {
element->SetAttribute(CNS_TAG_ATTR_X, (*it)->j);
element->SetAttribute(CNS_TAG_ATTR_Y, (*it)->i);
element->SetAttribute(CNS_TAG_ATTR_F, (*it)->rhs);
element->SetAttribute(CNS_TAG_ATTR_G, (*it)->g);
if ((*it)->g > 0){
element->SetAttribute(CNS_TAG_ATTR_PARX, (*it)->parent->j);
element->SetAttribute(CNS_TAG_ATTR_PARY, (*it)->parent->i);
}
child->InsertEndChild(element);
}
}
}
}
return element;
}