-
Notifications
You must be signed in to change notification settings - Fork 5
/
node.h
129 lines (108 loc) · 2.85 KB
/
node.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
#ifndef NODE_H
#define NODE_H
#include <memory>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <iostream>
struct Cell {
int x, y;
Cell() {}
Cell(int x_, int y_) : x(x_), y(y_) {}
Cell(Cell other, int x_, int y_) : x(other.x + x_), y(other.y + y_) {}
inline bool operator==(const Cell& p) const {
return x == p.x && y == p.y;
}
inline bool operator!=(const Cell& p) const {
return !(*this == p);
}
inline bool operator<(const Cell& p) const {
return y < p.y || (y == p.y && x < p.x);
}
Cell& operator=(const Cell& other) {
if (this == &other) {
return *this;
}
x = other.x;
y = other.y;
return *this;
}
Cell& operator+=(const Cell& other) {
x += other.x;
y += other.y;
return *this;
}
};
inline Cell operator+(const Cell& one, const Cell& other) {
Cell newc;
newc.x = one.x + other.x;
newc.y = one.y + other.y;
return newc;
}
inline std::ostream& operator<< (std::ostream& out, const Cell &next) {
out << "(" << next.x << "," << next.y << "); ";
return out;
}
struct Key{
double k1, k2;
Key() : k1(std::numeric_limits<double>::infinity()), k2(std::numeric_limits<double>::infinity()) {}
Key(double k1_, double k2_) : k1(k1_), k2(k2_) {}
Key(const Key& other) : k1(other.k1), k2(other.k2) {}
inline bool operator==(const Key& p) const {
return k1 == p.k1 && k2 == p.k2;
}
inline bool operator!=(const Key& p) const {
return !(*this == p);
}
inline bool operator<(const Key& p) const {
return k1 < p.k1 || (k1 == p.k1 && k2 <= p.k2);
}
Key& operator=(const Key& other) {
if (this == &other) {
return *this;
}
k1 = other.k1;
k2 = other.k2;
return *this;
}
};
class Node {
public:
double rhs, g;
Key key;
Cell cell;
Node *parent;
Node() {}
Node(const Cell& p, Node *c = nullptr) : g(std::numeric_limits<double>::infinity()),
rhs(std::numeric_limits<double>::infinity()), cell(p), parent(c) {}
Node& operator=(const Node& other) {
cell = other.cell;
rhs = other.rhs;
g = other.g;
parent = other.parent;
key = other.key;
return *this;
}
bool IsConsistent() const {
return g == rhs;
}
};
inline bool operator<(const Node& one, const Node& another) {
return one.cell.x < another.cell.x;
}
inline std::ostream& operator<< (std::ostream& out, const Node &next) {
out << "(" << next.cell.x << "," << next.cell.y << "); ";
return out;
}
struct ANode
{
int i, j;
double F, g, H;
ANode *parent;
bool operator== (const ANode &other) const {
return i == other.i && j == other.j;
}
};
#endif // NODE_H