aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
blob: 821c3e6e2f9c72a4b04c2bec0c6635d8d01f4fff (plain)
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
#ifndef UTIL_H
#define UTIL_H

#include <stdio.h> // fprintf()
#include <stdlib.h> // exit()

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define UNUSED(a) ((void) (a))

#define D(...) do { \
  fprintf(stderr, "DEBUG: %s(): ", __func__); \
  fprintf(stderr, __VA_ARGS__); \
  fputs("\n", stderr); \
} while (0)

#define die(...) do { \
  fputs("FATAL: ", stderr); \
  fprintf(stderr, __VA_ARGS__); \
  fputs("\n", stderr); \
  exit(EXIT_FAILURE); \
} while (0)

// calculate squared euclidean distance between two points
static inline float
distance_squared(
  const size_t num_floats,
  const float * const a,
  const float * const b
) {
  float r = 0.0;

  for (size_t i = 0; i < num_floats; i++) {
    r += (b[i] - a[i]) * (b[i] - a[i]);
  }

  // return squared distance
  return r;
}

static inline float
km_score(
  const float mean_distance,
  const size_t num_empty
) {
  return 1.0 / (mean_distance + num_empty);
}

#endif /* UTIL_H */