static int32_t counts_index_for(struct hdr_histogram* h, int64_t value) { int32_t bucket_index = get_bucket_index(h, value); int32_t sub_bucket_index = get_sub_bucket_index(value, bucket_index, h->unit_magnitude); return counts_index(h, bucket_index, sub_bucket_index); }
/// Add a key-value pair to a hash map. bool DS_Hash_Map_add(DS_Hash_Map_t *map, char *key, void *value){ MU_ARG_CHECK(logger, false, map, key); MU_COND_RWLOCK_WRLOCK(map->lock, logger); char trunc_key[DS_HASH_MAP_KEY_SIZE + 1]; snprintf(trunc_key, DS_HASH_MAP_KEY_SIZE + 1, "%s", key); size_t index = get_bucket_index(trunc_key, map->amount_of_buckets); DS_Bucket_t *bucket = map->buckets[index]; if(!bucket){ bucket = (map->buckets[index] = create_bucket(trunc_key, value, NULL)); if(!bucket){ MU_LOG_ERROR(logger, "DS_Hash_Map_add->create_bucket: \"Was unable to create a bucket!\""); MU_COND_RWLOCK_UNLOCK(map->lock, logger); return false; } bucket->in_use = 1; map->size++; MU_COND_RWLOCK_UNLOCK(map->lock, logger); return true; } void *key_exists = get_value_from_bucket(bucket, trunc_key); if(key_exists){ MU_COND_RWLOCK_UNLOCK(map->lock, logger); return false; } do { if(!bucket->in_use){ sprintf(bucket->key, "%s", trunc_key); bucket->value = value; bucket->in_use = 1; map->size++; break; } else if(!bucket->next){ bucket->next = create_bucket(trunc_key, value, NULL); bucket->next->in_use = 1; map->size++; break; } } while((bucket = bucket->next)); MU_COND_RWLOCK_UNLOCK(map->lock, logger); return true; }
void rebucket(uint32 count) { Bucket * new_buckets = new Bucket[count]; Bucket * end = buckets_ + bucket_count_; for (Bucket * bucket = buckets_; bucket != end; ++bucket) { Node * next = 0; for (Node * node = bucket->start; node; node = next) { next = node->next; Node * & start = new_buckets[get_bucket_index(node->key, count)].start; start = Node::insert(start, node); } } end = new_buckets + count; for (Bucket * bucket = new_buckets; bucket != end; ++bucket) { bucket->size = Node::size(bucket->start); } Bucket * temp = buckets_; buckets_ = new_buckets; bucket_count_ = count; delete [] temp; }
static DS_Bucket_t *get_bucket(DS_Bucket_t **buckets, size_t amount_of_buckets, const char *key){ return buckets[get_bucket_index(key, amount_of_buckets)]; }
Bucket * get_bucket(Key const & key) { return buckets_ + get_bucket_index(key); }
uint32 get_bucket_index(Key const & key) { return get_bucket_index(key, bucket_count_); }