diff --git a/src/collections.h b/src/collections.h index 0ff39fe4..9008dce7 100644 --- a/src/collections.h +++ b/src/collections.h @@ -24,6 +24,7 @@ KHASH_INIT(char_uint32, char, uint32_t, 1, kh_char_hash_func, kh_char_hash_equal KHASH_INIT(uchar_uint32, unsigned char, uint32_t, 1, kh_char_hash_func, kh_char_hash_equal) KHASH_MAP_INIT_STR(str_uint32, uint32_t) +KHASH_MAP_INIT_STR(str_double, double) KHASH_MAP_INIT_INT(int_str, char *) KHASH_MAP_INIT_STR(str_str, char *) diff --git a/src/features.c b/src/features.c index d0375888..782a5996 100644 --- a/src/features.c +++ b/src/features.c @@ -26,18 +26,29 @@ void feature_array_add_printf(cstring_array *features, char *format, ...) { } -bool feature_counts_update(khash_t(str_uint32) *features, char *feature, int count) { +bool feature_counts_update_or_add(khash_t(str_double) *features, char *feature, double count, bool add) { khiter_t k; - k = kh_get(str_uint32, features, feature); + k = kh_get(str_double, features, feature); if (k == kh_end(features)) { int ret; - k = kh_put(str_uint32, features, feature, &ret); + k = kh_put(str_double, features, feature, &ret); if (ret < 0) return false; kh_value(features, k) = count; - } else { + } else if (add) { kh_value(features, k) += count; + } else { + kh_value(features, k) = count; } return true; } + + +inline bool feature_counts_add(khash_t(str_double) *features, char *feature, double count) { + return feature_counts_update_or_add(features, feature, count, true); +} + +inline bool feature_counts_update(khash_t(str_double) *features, char *feature, double count) { + return feature_counts_update_or_add(features, feature, count, false); +} diff --git a/src/features.h b/src/features.h index d67d9ce5..97fbb3ec 100644 --- a/src/features.h +++ b/src/features.h @@ -17,6 +17,9 @@ void feature_array_add_printf(cstring_array *features, char *format, ...); // Add feature count to dictionary -bool feature_counts_update(khash_t(str_uint32) *features, char *feature, int count); +bool feature_counts_add(khash_t(str_double) *features, char *feature, double count); +bool feature_counts_update(khash_t(str_double) *features, char *feature, double count); + +VECTOR_INIT(feature_count_array, khash_t(str_double) *) #endif \ No newline at end of file