-
Notifications
You must be signed in to change notification settings - Fork 1
/
distanceToPeak.cpp
48 lines (41 loc) · 1.11 KB
/
distanceToPeak.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
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector distanceToPeakCpp(NumericVector distance, NumericVector rho) {
int size = rho.size();
NumericVector peaks(size);
NumericVector maximum(size);
int i = 0;
for (int col = 0; col < size; col++) {
for (int row = col + 1; row < size; row++) {
double newValue = distance[i];
double rhoRow = rho[row];
double rhoCol = rho[col];
if (rhoRow > rhoCol) {
double peaksCol = peaks[col];
if (newValue < peaksCol || peaksCol == 0) {
peaks[col] = newValue;
}
} else if (newValue > maximum[col]) {
maximum[col] = newValue;
}
if (rhoCol > rhoRow) {
double peaksRow = peaks[row];
if (newValue < peaksRow || peaksRow == 0) {
peaks[row] = newValue;
}
} else if (newValue > maximum[row]) {
maximum[row] = newValue;
}
i++;
}
}
for (int j = 0; j < size; j++) {
if (peaks[j] == 0) {
peaks[j] = maximum[j];
} else {
// do nothing, peaks is already min
}
}
return peaks;
}