forked from samlbest/traveling-salesman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
city.cpp
199 lines (160 loc) · 3.96 KB
/
city.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
186
187
188
189
190
191
192
193
194
195
196
197
198
// city.cpp - Implementation of the city class, which manages data relating
// to a single city. x is the x coord of the city, y is the y coord. Distance
// to other cities is calculated using the Euclidean distance formula rounded
// to the nearest integer.
// Written by Sam Best for CS325
// Prototypes of city class
// Last modified 3/12/2013
#include "city.h"
#include "tree.h"
#include <iostream>
#include <fstream>
using namespace std;
const int LIST_SIZE = 5;
// Constructor that takes all members as input
city::city(int idin, int xin, int yin, int posin, bool visitedin) : id(idin), x(xin), y(yin), pos(posin), visited(visitedin) {}
// Copy constructor
city::city(city & source)
{
x = source.x;
y = source.y;
pos = source.pos;
id = source.id;
visited = source.visited;
neighbor_list = source.neighbor_list;
}
// Destructor (no dynamic memory)
city::~city()
{
}
// Calculates the Euclidean distance to city_in
int city::dist(city * city_in)
{
float x2 = city_in->get_x();
float y2 = city_in->get_y();
x2 -= x;
y2 -= y;
x2 *= x2;
y2 *= y2;
return floor(sqrt(x2 + y2) + 0.5); // Rounds down from 0.0 to .499_, rounds up from 0.5 to .99_
}
int city::get_x()
{
return x;
}
int city::get_y()
{
return y;
}
int city::get_id()
{
return id;
}
int city::get_pos()
{
return pos;
}
city *& city::get_neighbor(int index)
{
return neighbor_list[index];
}
int city::get_nl_size()
{
return LIST_SIZE;
}
void city::set_pos(int pos_in)
{
pos = pos_in;
}
void city::display_coords()
{
cout << id << " " << x << " " << y;
return;
}
void city::output_id(ostream & output)
{
output << id;
}
// Writes to file that is already open, takes ofstream object as parameter
int city::write_out(ofstream & write)
{
if (write.is_open())
{
write << id << '\n';
return 1;
}
return 0;
}
// Copies in a city and its neighbor list
int city::copy_city(city * city_in)
{
if (city_in == NULL)
{
return 0;
}
id = city_in->id;
x = city_in->x;
y = city_in->y;
pos = city_in->pos;
visited = city_in->visited;
neighbor_list = city_in->neighbor_list;
return 1;
}
// Returns 1 if id, x, y are equivalent to those in city_in, otherwise returns 0
int city::compare(const city * city_in)
{
if (city_in->id == id && city_in->x == x && city_in->y == y)
{
return 1;
}
return 0;
}
// Builds a neighbor list from cities, sorted by distance
void city::build_neighbor_list(deque <city*> & cities, int num_cities)
{
tree sorted(this); // Creates a tree with the current city as owner (to calculate distances for sorting)
neighbor_list.clear(); // Erase existing neighbor list
for (int i = 0; i < num_cities; ++i)
{
sorted += cities[i]; // Add cities to BST
}
// Build neighbor list of size LIST_SIZE or num_cities, whichever is smaller
// (issues with this because my tree doesn't correctly sort when the recursive
// function is cut off before going through the whole tree)
if (LIST_SIZE < num_cities)
{
sorted.build_neighbor_list(neighbor_list, LIST_SIZE);
}
else
{
sorted.build_neighbor_list(neighbor_list, num_cities);
}
// sorted.display_detailed_tree();
cout << pos << " NEIGHBOR LIST SIZE: " << neighbor_list.size() << endl;
sorted.clear_tree();
return;
}
bool city::nl_is_empty()
{
return neighbor_list.empty();
}
void city::push_to_list(city *& to_add)
{
neighbor_list.push_back(new city(*to_add));
}
void city::display_neighbor_list()
{
int size = neighbor_list.size();
for (int i = 0; i < size; ++i)
{
cout << dist(neighbor_list[i]);
cout << "actual index: " << i;
cout << " stored pos: " << neighbor_list[i]->get_pos();
cout << endl;
}
return;
}
int city::get_neighbor_pos(int index)
{
return neighbor_list[index]->pos;
}