-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath_array.cpp
More file actions
99 lines (81 loc) · 2.43 KB
/
Path_array.cpp
File metadata and controls
99 lines (81 loc) · 2.43 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
#include "Path_array.hpp"
#include <limits>
#include <iostream>
Path_array::Path_array() {
this->path_array = new Path*[50];
this->size = 50;
this->count = 0;
for( int i{0}; i < this->size; i++ ) {
this->path_array[i] = nullptr;
}
}
Path_array::~Path_array() {
for( int i{0}; i < this->count; i++ ) {
if( this->path_array[i] != nullptr) {
delete this->path_array[i];
this->path_array[i] = nullptr;
}
}
delete[] this->path_array;
}
int Path_array::get_count() {
return this->count;
}
void Path_array::insert( Path* path ) {
if( this->count >= this->size - 3 ) {
this->resize();
}
this->path_array[this->count++] = path;
}
void Path_array::resize() {
int new_size = this->size * 2;
Path** new_array = new Path*[new_size]();
for( int i{0}; i < this->count; i++ ) {
new_array[i] = this->path_array[i];
}
delete[] this->path_array;
this->path_array= new_array;
this->size = new_size;
}
void Path_array::delete_path( Path* path ) {
for( int i{0}; i < this->count; i++ ) {
if( this->path_array[i] != nullptr && this->path_array[i] == path ) {
delete this->path_array[i];
for( int j{i}; j < this->count - 1; j++ ) {
this->path_array[j] = this->path_array[j + 1];
}
this->count--;
}
}
}
Path* Path_array::min_weight_path() {
double min_weight = __DBL_MAX__;
Path* min_path = nullptr;
for( int i{0}; i < this->count; i++ ) {
if( this->path_array[i]->get_weight() < min_weight ) {
min_weight = this->path_array[i]->get_weight();
min_path = this->path_array[i];
}
}
return min_path;
}
void Path_array::remove_path( Path* path ) {
for( int i{0}; i < this->count; i++ ) {
if( this->path_array[i] != nullptr && this->path_array[i] == path ) {
this->path_array[i] = nullptr;
for( int j{i}; j < this->count - 1; j++ ) {
this->path_array[j] = this->path_array[j + 1];
}
this->count--;
this->path_array[this->count] = nullptr;
}
}
}
bool Path_array::last_node_in_path_array( int last_node ) {
for( int i{0}; i < this->count; i++ ) {
if( this->path_array[i] != nullptr && this->path_array[i]->get_last_node() == last_node ) {
return true;
}
}
return false;
}