-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeighbors.cpp
More file actions
67 lines (57 loc) · 1.49 KB
/
Neighbors.cpp
File metadata and controls
67 lines (57 loc) · 1.49 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include "FreqVector.hpp"
#include "Neighbors.hpp"
#include <vector>
#include <cmath>
Neighbors::Neighbors(){}
void Neighbors::AddVector(FreqVector new_vector)
{
vectors.push_back(new_vector);
}
//this is a really naive approach
//maybe I'll make it faster later
vector<FreqVector> Neighbors::KNN(FreqVector query, int k)
{
//sanity test:
if(k > vectors.size())
{
printf("Need more vectors in Neighbors\n");
}
//allocate a bit of extra space so we don't have out of bounds accesses
//just simplifies our loop later
int * nearest_k_indicies = (int *) malloc(sizeof(int) * (k + 1));
double * nearest_k_distances = (double *) malloc(sizeof(double) * (k + 1));
for(int j = 0; j < k; j++)
{
nearest_k_distances[j] = 10000000000000000.0;
nearest_k_indicies[j] = -1;
}
for(int i = 0; i < vectors.size(); i++)
{
int dist = query.distance(vectors[i]);
for(int j = 0; j < k; j++)
{
if(dist < nearest_k_distances[j])
{
//shift all the values back one
//extra space makes this not go out of bounds
for(int j2 = j; j2 < k; j2++)
{
nearest_k_distances[j2 + 1] = nearest_k_distances[j2];
nearest_k_indicies[j2 + 1] = nearest_k_indicies[j2];
}
nearest_k_distances[j] = dist;
nearest_k_indicies[j] = i;
}
}
}
vector<FreqVector> k_nearest_neighbors;
for(int j = 0; j < k; j++)
{
k_nearest_neighbors.push_back(vectors[nearest_k_indicies[j]]);
}
return k_nearest_neighbors;
}