-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity_metrics.cpp
70 lines (53 loc) · 2.28 KB
/
similarity_metrics.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
#include "similarity_metrics.h"
#include <iostream>
void calculateIntersectionAndUnionSizes( const struct targs& argument1, const struct targs& argument2, const struct pargs& program_arguments, size_t& intersectionSize, size_t& unionSize ) {
std::vector<uint32_t>::const_iterator it_core1 = argument1.cores.begin(), it_core2 = argument2.cores.begin();
std::vector<uint32_t>::const_iterator it_end1 = argument1.cores.end(), it_end2 = argument2.cores.end();
intersectionSize = 0;
unionSize = 0;
bool countDistinct = ( program_arguments.type == SET );
while ( it_core1 < it_end1 && it_core2 < it_end2 ) {
unionSize++;
if ( *it_core1 == *it_core2 ) {
intersectionSize++;
it_core1++;
it_core2++;
if (countDistinct) {
while ( it_core1 < it_end1 && *(it_core1-1) == *(it_core1)) it_core1++;
while ( it_core2 < it_end2 && *(it_core2-1) == *(it_core2)) it_core2++;
}
} else if ( *it_core1 < *it_core2 ) {
it_core1++;
if (countDistinct) {
while ( it_core1 < it_end1 && *(it_core1-1) == *(it_core1)) it_core1++;
} else {
it_core1++;
}
} else {
it_core2++;
if (countDistinct) {
while ( it_core2 < it_end2 && *(it_core2-1) == *(it_core2)) it_core2++;
}
}
}
while ( it_core1 < it_end1 ) {
unionSize++;
it_core1++;
if (countDistinct) {
while ( it_core1 < it_end1 && *(it_core1-1) == *(it_core1)) it_core1++;
}
}
while ( it_core2 < it_end2 ) {
unionSize++;
it_core2++;
if (countDistinct) {
while ( it_core2 < it_end2 && *(it_core2-1) == *(it_core2)) it_core2++;
}
}
};
double calculateJaccardSimilarity( size_t intersectionSize, size_t unionSize ) {
return static_cast<double>(intersectionSize) / static_cast<double>(unionSize);
};
double calculateDiceSimilarity( const size_t intersectionSize, const struct targs& argument1, const struct targs& argument2 ) {
return 2 * static_cast<double>(intersectionSize) / ( static_cast<double>(argument1.core_count) + static_cast<double>(argument2.core_count) );
};