33 #ifndef UTIL_GTL_HASHTABLE_COMMON_H_ 34 #define UTIL_GTL_HASHTABLE_COMMON_H_ 41 template<
typename Key,
typename HashFunc,
42 typename SizeType,
int HT_MIN_BUCKETS>
43 class sh_hashtable_settings :
public HashFunc {
46 typedef HashFunc hasher;
47 typedef SizeType size_type;
50 sh_hashtable_settings(
const hasher& hf,
51 const float ht_occupancy_flt,
52 const float ht_empty_flt)
54 enlarge_threshold_(0),
56 consider_shrink_(false),
60 set_enlarge_factor(ht_occupancy_flt);
61 set_shrink_factor(ht_empty_flt);
64 size_type hash(
const key_type& v)
const {
65 return hasher::operator()(v);
68 float enlarge_factor()
const {
69 return enlarge_factor_;
71 void set_enlarge_factor(
float f) {
74 float shrink_factor()
const {
75 return shrink_factor_;
77 void set_shrink_factor(
float f) {
81 size_type enlarge_threshold()
const {
82 return enlarge_threshold_;
84 void set_enlarge_threshold(size_type t) {
85 enlarge_threshold_ = t;
87 size_type shrink_threshold()
const {
88 return shrink_threshold_;
90 void set_shrink_threshold(size_type t) {
91 shrink_threshold_ = t;
94 size_type enlarge_size(size_type x)
const {
95 return static_cast<size_type
>(x * enlarge_factor_);
97 size_type shrink_size(size_type x)
const {
98 return static_cast<size_type
>(x * shrink_factor_);
101 bool consider_shrink()
const {
102 return consider_shrink_;
104 void set_consider_shrink(
bool t) {
105 consider_shrink_ = t;
108 bool use_empty()
const {
111 void set_use_empty(
bool t) {
115 bool use_deleted()
const {
118 void set_use_deleted(
bool t) {
122 size_type num_ht_copies()
const {
123 return static_cast<size_type
>(num_ht_copies_);
125 void inc_num_ht_copies() {
130 void reset_thresholds(size_type num_buckets) {
131 set_enlarge_threshold(enlarge_size(num_buckets));
132 set_shrink_threshold(shrink_size(num_buckets));
134 set_consider_shrink(
false);
139 void set_resizing_parameters(
float shrink,
float grow) {
140 assert(shrink >= 0.0);
142 if (shrink > grow/2.0f)
143 shrink = grow / 2.0f;
144 set_shrink_factor(shrink);
145 set_enlarge_factor(grow);
150 size_type min_buckets(size_type num_elts, size_type min_buckets_wanted) {
151 float enlarge = enlarge_factor();
152 size_type sz = HT_MIN_BUCKETS;
153 while ( sz < min_buckets_wanted ||
154 num_elts >= static_cast<size_type>(sz * enlarge) ) {
157 if (static_cast<size_type>(sz * 2) < sz) {
158 throw std::length_error(
"resize overflow");
166 size_type enlarge_threshold_;
167 size_type shrink_threshold_;
168 float enlarge_factor_;
169 float shrink_factor_;
171 bool consider_shrink_;
175 unsigned int num_ht_copies_;
178 #endif // UTIL_GTL_HASHTABLE_COMMON_H_