[features] Using a str=>double hashtable for feature counts

This commit is contained in:
Al
2015-12-28 01:18:49 -05:00
parent e4dba2297d
commit 72ad01cbc3
3 changed files with 20 additions and 5 deletions

View File

@@ -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 *)

View File

@@ -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);
}

View File

@@ -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