-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.cpp
185 lines (168 loc) · 3.81 KB
/
types.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <cmath>
#include <iostream>
#include <limits>
#include "types.h"
aco::Node::Node(double x, double y, int id)
{
this->id = id;
this->x = x;
this->y = y;
}
bool aco::Node::operator==(const aco::Node &other) const
{
return this->id == other.id;
}
/**
* Get distance between current node and node with node id as node_id
* @param node_id
* @return distance
*/
double aco::Node::get_distance_from_neighbor(int node_id) const
{
for(const auto& neighbor : this->neighbors)
{
if(neighbor.first == node_id)
{
return neighbor.second;
}
}
return std::numeric_limits<double>::max();
}
/**
* Construct the graph
*/
aco::Graph::Graph()
{
n_nodes_ = 0;
}
/**
* Generic begin std iterator for aco::Graph
* @return begin iterator
*/
aco::Graph::iterator aco::Graph::begin()
{
return graph_.begin();
}
/**
* Generic end std iterator for aco::Graph
* @return end iterator
*/
aco::Graph::iterator aco::Graph::end()
{
return graph_.end();
}
/**
* Generic const begin std iterator for aco::Graph
* @return c begin iterator
*/
aco::Graph::const_iterator aco::Graph::cbegin() const
{
return graph_.cbegin();
}
/**
* Generic const end std iterator for aco::Graph
* @return c end iterator
*/
aco::Graph::const_iterator aco::Graph::cend() const
{
return graph_.cend();
}
/**
* Get the no. of nodes in the graph
* @return
*/
int aco::Graph::size() const
{
return n_nodes_;
}
/**
* Get the mean of all edge weights in the graph
* @return
*/
double aco::Graph::mean_edge_weight() const
{
double sum_edge_weights = 0;
int n_edges = 0;
for(const auto& node: graph_)
{
for(const auto& neighbor : node.neighbors)
{
sum_edge_weights += neighbor.second;
n_edges++;
}
}
return (sum_edge_weights)/(n_edges);
}
/**
* Create a new node in Graph with co-ordinates x and y
* @param x - x coordinate
* @param y - y coordinate
* @return
*/
int aco::Graph::create_node_in_graph(double x, double y)
{
const int id = n_nodes_++;
this->graph_.emplace_back(Node(x, y, id));
return id;
}
/**
* Add edge (onw way) from node_id_from to node_id_to in the graph
* @param node_id_from
* @param node_id_to
*/
void aco::Graph::add_edge(int node_id_from, int node_id_to)
{
const auto node_from = this->get_node_from_graph(node_id_from);
if(node_from == nullptr)
{
std::cout << "Cannot add edge. Node (from) not present in the graph. \n";
return;
}
const auto node_to = this->get_node_from_graph(node_id_to);
if(node_to == nullptr)
{
std::cout << "Cannot add edge. Node (to) not present in the graph. \n";
return;
}
int neighbor_id = node_to->id;
double distance = sqrt(pow((node_from->x - node_to->x), 2) + pow((node_from->y - node_to->y), 2));
node_from->neighbors.emplace_back(std::make_pair(neighbor_id, distance));
}
/**
* Get the node from the graph using node id
* @param node_id - id of the node
* @return Node
*/
aco::Node aco::Graph::get_node_from_graph(int node_id) const
{
return graph_.at(node_id);
}
/**
* Get pointer to the node from the graph using node id
* @param node_id - id of the node
* @return Node*
*/
aco::Node* aco::Graph::get_node_from_graph(int node_id)
{
return &graph_.at(node_id);
}
namespace std
{
/**
* Add hash for aco::Node
*/
template <>
struct hash<aco::Node>
{
std::size_t operator()(const aco::Node& k) const
{
using std::size_t;
using std::hash;
using std::string;
// Compute individual hash values for first,
// second and third and combine them using XOR
// and bit shifting:
return ((hash<int>()(k.id) << 1) >> 1) ^ (hash<int>()(k.id) << 1);
}
};
}