forked from magnusolavhelland/CSD-Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
157 lines (120 loc) · 2.68 KB
/
utils.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
#include <algorithm>
#include "utils.h"
#include "myStructs.h"
#include <iostream> //remove after debug
using namespace std;
typedef double* DblArr;
void allocateMemory(double ***matrix, const int& dim1, const int& dim2, const int& dim3)
{
matrix = new DblArr*[dim1];
for (int i=0; i<dim2; ++i)
{
matrix[i] = new DblArr[dim2];
for (int j=0; j<dim3; ++j)
{
matrix[i][j] = new double[dim3];
}
}
}
void allocateMemory(double **matrix, const int& dim1, const int& dim2)
{
matrix = new DblArr[dim1];
for (int i=0; i<dim1; ++i) matrix[i] = new double[dim2];
}
void freeMemory(double ***matrix, const int& dim1, const int& dim2)
{
for (int i=0; i<dim1; ++i)
{
for (int j=0; j<dim2; ++j)
{
delete[] matrix[i][j];
}
delete[] matrix[i];
}
delete[] matrix;
}
void freeMemory(double **matrix, const int& dim1)
{
for (int i=0; i<dim1; ++i) delete[] matrix[i];
delete[] matrix;
}
bool geneLessThan(const Gene& a, const Gene& b)
{
return a.geneID < b.geneID;
}
bool geneCompare(const Gene& a, const Gene& b)
{
return a.geneID <= b.geneID;
}
bool indexCompare(const Gene& g1, const Gene& g2)
{
return g1.index <= g2.index;
}
bool sortedPosCompare(const Gene& a, const Gene& b)
{
return a.sortedPos <= b.sortedPos;
}
int numberOfCombinations(const int& numberOfFiles)
{
int combos = 0;
for (int i = 0; i<numberOfFiles; ++i) combos += i;
return combos;
}
int invNumberOfCombinations(int m)
{
int n=0;
while (m>0)
{
++n;
m-=n;
}
if (m!=0)
{
cout << "Error: Invalid input to function invNumberOfCombinations()."
<< "Input number must represent the number of combinations between an integer"
<< "number of objects.\n";
exit(1);
}
return n+1;
}
void quickSortGenes(vector<Gene>& A, int p, int r, bool (*compare)(const Gene&, const Gene&))
{
if (p<r)
{
int q = partitionGenes(A, p, r, compare);
quickSortGenes(A, p, q-1, compare);
quickSortGenes(A, q+1, r, compare);
}
}
int partitionGenes(vector<Gene>& A, int p, int r, bool (*compare)(const Gene&, const Gene&))
{
int i = p-1;
for (int j=p; j<r; ++j)
{
if (compare(A[j],A[r]))
{
++i;
//iter_swap(A.begin()+i, A.begin()+j);
geneSwap(A[i], A[j]);
}
}
geneSwap(A[i+1], A[r]);
//iter_swap(A[i+1], A[r]);
return i+1;
}
void geneSwap(Gene& a, Gene& b)
{
Gene temp;
temp.geneID = a.geneID;
temp.index = a.index;
temp.sortedPos = a.sortedPos;
temp.origin = a.origin;
a.geneID = b.geneID;
a.index = b.index;
a.sortedPos = b.sortedPos;
a.origin = b.origin;
b.geneID = temp.geneID;
b.index = temp.index;
b.sortedPos = temp.sortedPos;
b.origin = temp.origin;
}