aboutsummaryrefslogtreecommitdiff
path: root/km-set.c
blob: ca028f8c73db724c49b1cdb0dbb7e3c79f9ea198 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <stdbool.h> // bool
#include <stdint.h> // size_t
#include <string.h> // memcpy()
#include <stdlib.h> // rand()
#include <errno.h> // errno
#include "util.h"
#include "km.h"

#define MIN_ROWS (4096 / sizeof(float))

// grow data set
static bool
km_set_grow(
  km_set_t * const set,
  const size_t capacity
) {
  float *floats = NULL;
  const size_t floats_size = sizeof(float) * set->shape.num_floats * capacity;
  // fprintf(stderr, "floats_size = %zu\n", floats_size);
  if (floats_size > 0) {
    // alloc floats
    floats = realloc(set->floats, floats_size);
    if (!floats) {
      // return failure
      return false;
    }
  }

  int *ints = NULL;
  const size_t ints_size = sizeof(int) * set->shape.num_ints * capacity;
  // fprintf(stderr, "ints_size = %zu\n", ints_size);
  if (ints_size > 0) {
    // alloc ints
    ints = realloc(set->ints, ints_size);
    if (!ints) {
      // return failure
      return false;
    }
  }

  // update set
  set->floats = floats;
  set->ints = ints;
  set->capacity = capacity;

  // return success
  return true;
}

// init data set with shape and initial size
bool
km_set_init(
  km_set_t * const set,
  const km_shape_t * const shape,
  const size_t row_capacity
) {
  // alloc bounds
  float * const bounds = malloc(2 * sizeof(float) * shape->num_floats);
  if (!bounds) {
    // return error
    return false;
  }

  set->state = KM_SET_STATE_INIT;
  set->floats = NULL;
  set->ints = NULL;
  set->shape = *shape;
  set->num_rows = 0;
  set->capacity = 0;
  set->bounds = bounds;

  return km_set_grow(set, MAX(MIN_ROWS, row_capacity + 1));
}

// finalize data set
void
km_set_fini(km_set_t * const set) {
  if (set->state == KM_SET_STATE_FINI) {
    return;
  }

  if (set->bounds) {
    // free bounds
    free(set->bounds);
    set->bounds = NULL;
  }

  if (set->floats) {
    // free floats
    free(set->floats);
    set->floats = NULL;
  }

  if (set->ints) {
    // free ints
    free(set->ints);
    set->ints = NULL;
  }

  // shrink capacity
  set->capacity = 0;

  // set state
  set->state = KM_SET_STATE_FINI;
}

// append rows to data set, growing set if necessary
bool
km_set_push(
  km_set_t * const set,
  const size_t num_rows,
  const float * const floats,
  const int * const ints
) {
  // check state
  if (set->state != KM_SET_STATE_INIT) {
    // return failure
    return false;
  }

  const size_t capacity_needed = set->num_rows + num_rows;
  // FIXME: potential overflow here
  if (capacity_needed >= set->capacity) {
    // crappy growth algorithm
    const size_t new_capacity = 2 * capacity_needed + 1;

    // resize storage
    if (!km_set_grow(set, MAX(MIN_ROWS, new_capacity))) {
      return false;
    }
  }

  // copy floats
  const size_t num_floats = set->shape.num_floats;
  if (num_floats > 0) {
    float * const dst = set->floats + num_floats * set->num_rows;
    const size_t stride = sizeof(float) * num_floats;

    // copy floats
    memcpy(dst, floats, stride * num_rows);

    if (!set->num_rows) {
      // there were no rows, so populate bounds with first row
      memcpy(set->bounds, floats, stride);
      memcpy(set->bounds + num_floats, floats, stride);
    }

    for (size_t i = 0; i < num_rows; i++) {
      for (size_t j = 0; j < num_floats; j++) {
        const float val = floats[i * num_floats + j];

        if (val < set->bounds[j]) {
          // update min bound
          set->bounds[j] = val;
        }

        if (val > set->bounds[num_floats + j]) {
          // update max bound
          set->bounds[num_floats + j] = val;
        }
      }
    }
  }

  // copy ints
  const size_t num_ints = set->shape.num_ints;
  if (num_ints > 0) {
    int * const dst = set->ints + num_ints * set->num_rows;
    const size_t stride = sizeof(int) * num_ints;

    // copy ints
    memcpy(dst, ints, stride * num_rows);
  }

  // increment row count
  set->num_rows += num_rows;

  // return success
  return true;
}

bool
km_set_copy(
  km_set_t * const dst,
  const km_set_t * const src
) {
  if (src->state != KM_SET_STATE_INIT || src->state != KM_SET_STATE_NORMALIZED) {
    // return failure
    return false;
  }

  // init dst set
  if (!km_set_init(dst, &(src->shape), src->num_rows)) {
    // return failure
    return false;
  }

  // copy floats
  const size_t num_floats = src->shape.num_floats;
  if (num_floats > 0) {
    const size_t stride = sizeof(float) * num_floats;

    // copy floats
    memcpy(dst->floats, src->floats, stride * src->num_rows);

    // copy bounds
    memcpy(dst->bounds, src->bounds, 2 * stride);
  }

  // copy ints
  const size_t num_ints = src->shape.num_ints;
  if (num_ints > 0) {
    const size_t stride = sizeof(int) * num_ints;

    // copy ints
    memcpy(dst->ints, src->ints, stride * src->num_rows);
  }

  // increment row count
  dst->num_rows = src->num_rows;

  // return success
  return true;
}

bool
km_set_normalize(
  km_set_t * const set
) {
  const size_t num_floats = set->shape.num_floats;

  // check set state
  if (set->state != KM_SET_STATE_INIT) {
    // return failure
    return false;
  }

  // normalize values
  for (size_t i = 0; i < set->num_rows; i++) {
    for (size_t j = 0; j < num_floats; j++) {
      const size_t ofs = i * num_floats + j;
      const float val = set->floats[ofs],
                  min = set->bounds[j],
                  max = set->bounds[num_floats + j];

      // normalize and write value
      set->floats[ofs] = (val - min) / (max - min);
    }
  }

  // set state
  set->state = KM_SET_STATE_NORMALIZED;

  // return success
  return true;
}

// get row from data set
float *
km_set_get_row(
  const km_set_t * const set,
  const size_t i
) {
  const size_t num_floats = set->shape.num_floats;
  return set->floats + i * num_floats;
}

// init a set with num_clusters clusters of shape num_floats by picking
// random cluster centers
bool
km_set_init_rand_clusters(
  km_set_t * const cs,
  const size_t num_floats,
  const size_t num_clusters,
  km_rand_t * const rs
) {
  // init cluster shape
  const km_shape_t shape = {
    .num_floats = num_floats,
    .num_ints = 1,
  };

  // generate random cluster centers
  float floats[num_floats * num_clusters];
  if (!km_rand_fill(rs, num_floats * num_clusters, floats)) {
    // return failure
    return false;
  }

  // FIXME: should probably be heap-allocated
  int ints[num_clusters];
  memset(ints, 0, sizeof(ints));

  // init cluster set
  if (!km_set_init(cs, &shape, num_clusters)) {
    // return failure
    return false;
  }

  // add data, return result
  return km_set_push(cs, num_clusters, floats, ints);
}