forked from parallella/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_mode.c
42 lines (38 loc) · 1014 Bytes
/
p_mode.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 <pal.h>
/**
*
* Calculates the mode value of input vector 'a'.
*
* @param a Pointer to input vector
*
* @param c Pointer to output scalar
*
* @param n Size of 'a' vector.
*
* @return None
*
*/
void PSYM(p_mode)(const PTYPE *a, PTYPE *c, int n)
{
unsigned int occurrence_count = 0;
unsigned int max_occurrence_count = 0;
unsigned int i = 1;
PTYPE mode_value = PCONST(0.0);
PTYPE *sorted_a = (PTYPE*) alloca(sizeof(PTYPE) * n);
PSYM(p_sort)(a, sorted_a, n);
for (; i < n; ++i) {
++occurrence_count;
if (sorted_a[i] != sorted_a[i - 1]) {
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[i - 1];
}
occurrence_count = 0;
}
}
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[n - 1];
}
*c = mode_value;
}