aboutsummaryrefslogtreecommitdiff
path: root/km-init-type.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-02-03 18:36:52 -0500
committerPaul Duncan <pabs@pablotron.org>2019-02-03 18:36:52 -0500
commit33a722132491ebdd31722f0cada0f81f6b082282 (patch)
tree389ae93db5d0a1ff8085ea5f38026d5e060deea1 /km-init-type.c
parentaa74bd04f66217ff4d617924630b13d721578159 (diff)
downloadkmeans-33a722132491ebdd31722f0cada0f81f6b082282.tar.bz2
kmeans-33a722132491ebdd31722f0cada0f81f6b082282.zip
cluster init refactoring, fix best sort, add km_score()
Diffstat (limited to 'km-init-type.c')
-rw-r--r--km-init-type.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/km-init-type.c b/km-init-type.c
new file mode 100644
index 0000000..27fc1ee
--- /dev/null
+++ b/km-init-type.c
@@ -0,0 +1,32 @@
+#include <stddef.h> // size_t
+#include <string.h> // strcmp()
+#include "km.h"
+
+static const struct {
+ const km_init_type_t type;
+ const char * const name;
+} TYPES[] = {{
+ .name = "rand",
+ .type = KM_INIT_TYPE_RAND,
+}, {
+ .name = "forgy",
+ .type = KM_INIT_TYPE_FORGY,
+}};
+
+#define NUM_TYPES (sizeof(TYPES) / sizeof(TYPES[0]))
+
+km_init_type_t
+km_get_init_type(
+ const char * const s
+) {
+ // find init method
+ for (size_t i = 0; i < NUM_TYPES; i++) {
+ if (!strcmp(s, TYPES[i].name)) {
+ // return type
+ return TYPES[i].type;
+ }
+ }
+
+ // return failure
+ return KM_INIT_TYPE_LAST;
+}