-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNearestNeighbours.c
42 lines (33 loc) · 1.03 KB
/
NearestNeighbours.c
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
#include "NearestNeighbours.h"
#include "BoundedPriorityQueue.h"
#include "DynamicTimeWarping.h"
#include "math.h"
#include <stdlib.h>
/**
* @author Lei Yang
* @author Simon Gardier
*/
SketchDistance *nearestNeighbours(const Dataset *dataset, Sketch query, size_t k)
{
BoundedPriorityQueue *q = bpqCreate(k);
bpqInsert(q, INFINITY, 0);
if (!q)
return NULL;
double distancesArray[dataset->size];
for (unsigned int i = 0; i < dataset->size; i++)
{
double distanceBetween = dtw(query, dataset->sketches[i], bpqMaximumKey(q));
distancesArray[i] = distanceBetween;
if (!bpqInsert(q, distanceBetween, i) && distanceBetween < bpqMaximumKey(q))
bpqReplaceMaximum(q, distanceBetween, i);
}
size_t *queueData = bpqGetItems(q);
SketchDistance *nearestSketches = malloc(sizeof(SketchDistance) * k);
for (unsigned int i = 0; i < k; i++)
{
nearestSketches[i].distance = distancesArray[queueData[i]];
nearestSketches[i].sketch = &(dataset->sketches[queueData[i]]);
}
bpqFree(q);
return nearestSketches;
}