-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathKnnCache.c
120 lines (94 loc) · 3.31 KB
/
KnnCache.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// KnnCache.c
// implementation of KnnCache.h
// stores the cached values in an array sized to hold every possible key
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "KnnCache.h"
#define T KnnCache_T
////////////////////////////////////////////////////////////////////////////////
// static functions
////////////////////////////////////////////////////////////////////////////////
/*
static void fail(char * msg) {
fprintf(stderr, "%s\n", msg);
exit(1);
}
*/
////////////////////////////////////////////////////////////////////////////////
// KnnCache_new
////////////////////////////////////////////////////////////////////////////////
// return pointer to struct KnnCache_T that can hold size entries
T KnnCache_new(unsigned size) {
static int debug = 0;
T knnCache = malloc(sizeof(struct T));
assert(knnCache);
// initialize field size
knnCache->size = size;
// initialize field neighborsP
unsigned** vectorP = malloc(sizeof(unsigned *) * size);
assert(vectorP);
knnCache->neighborsP = vectorP;
if (debug) printf("KnnCache_new: knnCache=%p\n", (void*)knnCache);
if (debug) printf("KnnCache_new: knnCache->neighborsP=%p\n",
(void*)knnCache->neighborsP);
for (unsigned i = 0; i < size; i++) {
vectorP[i] = NULL;
if (debug) printf("knnCache_new: vectorP[%u]=%p\n", i, (void*)vectorP[i]);
}
return knnCache;
}
////////////////////////////////////////////////////////////////////////////////
// KnnCache_free
////////////////////////////////////////////////////////////////////////////////
// free the memory
void KnnCache_free(T *knnCacheP) {
assert(knnCacheP);
assert(*knnCacheP);
T knnCache = *knnCacheP;
// free any vectors of unsigned's
unsigned **vectorP = knnCache->neighborsP;
for (unsigned i = 0; i < knnCache->size; i++) {
free(vectorP[i]);
}
free(vectorP);
free(knnCache);
// set argument to NULL
*knnCacheP = NULL;
}
////////////////////////////////////////////////////////////////////////////////
// KnnCache_get
////////////////////////////////////////////////////////////////////////////////
// retrieve a value using key queryIndex
unsigned *KnnCache_get(T knnCache,
const unsigned queryIndex) {
const int debug = 0;
assert(knnCache);
assert(queryIndex < knnCache->size);
if (debug) printf("queryIndex=%u\n", queryIndex);
unsigned *result = knnCache->neighborsP[queryIndex];
return result;
}
////////////////////////////////////////////////////////////////////////////////
// KnnCache_set
////////////////////////////////////////////////////////////////////////////////
// cache[key] = copy of values
void KnnCache_set(T knnCache,
const unsigned key,
const unsigned valuesSize,
const unsigned values[valuesSize]) {
const int debug = 0;
const unsigned debugKey = 262712;
if (debug && key == debugKey) printf("set values[0]=%u\n", values[0]);
if (debug && key == debugKey) printf("set knnCache=%p\n", (void*)knnCache);
assert(knnCache);
assert(valuesSize > 0);
assert(key < knnCache->size);
unsigned *memP = malloc(sizeof(unsigned) * valuesSize);
assert(memP);
for (unsigned i = 0; i < valuesSize; i++) {
if (debug) printf("values[%u]=%u\n", i, values[i]);
memP[i] = values[i];
}
knnCache->neighborsP[key] = memP;
}