-
Notifications
You must be signed in to change notification settings - Fork 20
/
Queues.cpp
162 lines (139 loc) · 3.76 KB
/
Queues.cpp
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
#include "Queues.h"
#include "node.h"
#include "gl_const.h"
bool iOpen::less(const Node &x, const Node &y) const {
if (x.F == y.F) {
switch (breaking_tie) {
case CN_SP_BT_GMIN:
return x.g < y.g;
case CN_SP_BT_GMAX:
default:
return x.g > y.g;
}
}
return x.F < y.F;
}
SortedList::SortedList(size_t size, int breakingtie) : data(size), size_(0), min_pos(size) {
breaking_tie = breakingtie;
}
size_t SortedList::size() const {
return size_;
}
bool SortedList::empty() const {
return (size_ == 0);
}
bool SortedList::Insert(const Node &newNode) {
std::list<Node>::iterator iter, pos;
bool posFound = false;
pos = data[newNode.i].end();
if (data[newNode.i].empty()) {
data[newNode.i].push_back(newNode);
++size_;
return true;
}
for (iter = data[newNode.i].begin(); iter != data[newNode.i].end(); ++iter) {
if (!posFound && !less(*iter, newNode)) {
pos = iter;
posFound = true;
}
if (iter->j == newNode.j && iter->z == newNode.z) {
if (newNode.F >= iter->F) {
return false;
} else {
if (pos == iter) {
iter->g = newNode.g;
iter->F = newNode.F;
iter->H = newNode.H;
return true;
}
data[newNode.i].erase(iter);
--size_;
break;
}
}
}
++size_;
data[newNode.i].insert(pos, newNode);
return true;
}
Node SortedList::FindMin() const {
Node min;
min.F = -1;
for (size_t i = 0; i < data.size(); i++) {
if (!data[i].empty())
if (min.F == -1 || less(*data[i].begin(), min)) {
min = *data[i].begin();
min_pos = i;
}
}
return min;
}
void SortedList::DeleteMin() {
if (min_pos >= data.size()) {
FindMin();
}
data[min_pos].pop_front();
--size_;
min_pos = data.size();
}
ClusteredSets::ClusteredSets(size_t size, int breakingtie) : loc_mins(size), data(size), size_(0), min_pos(size) {
breaking_tie = breakingtie;
}
size_t ClusteredSets::size() const {
return size_;
}
bool ClusteredSets::empty() const {
return (size_ == 0);
}
bool ClusteredSets::Insert(const Node &NewNode) {
bool node_found = false;
bool updated = false;
auto pos = data[NewNode.i].find(NewNode);
if (pos != data[NewNode.i].end()) {
node_found = true;
if (NewNode.g < pos->g) {
data[NewNode.i].erase(pos);
data[NewNode.i].insert(NewNode);
updated = true;
} else {
return false;
}
}
if (!node_found) {
updated = true;
data[NewNode.i].insert(NewNode);
++size_;
}
if (data[NewNode.i].size() == 1 || less(NewNode, loc_mins[NewNode.i])) {
loc_mins[NewNode.i] = NewNode;
}
return updated;
}
Node ClusteredSets::FindMin() const {
for (min_pos = 0; data[min_pos].empty(); ++min_pos) {}
for (size_t i = min_pos + 1; i < loc_mins.size(); ++i) {
if (!data[i].empty() && less(loc_mins[i], loc_mins[min_pos])) {
min_pos = i;
}
}
return loc_mins[min_pos];
}
void ClusteredSets::DeleteMin() {
if (min_pos == loc_mins.size()) {
FindMin();
}
data[min_pos].erase(loc_mins[min_pos]);
--size_;
Node min;
if (!data[min_pos].empty()) {
auto it = data[min_pos].begin();
min = *(it++);
for (; it != data[min_pos].end(); ++it) {
if (less(*it, min)) {
min = *it;
}
}
loc_mins[min_pos] = min;
}
min_pos = loc_mins.size();
}