52 #ifndef IFPACK_HASHTABLE_H
53 #define IFPACK_HASHTABLE_H
55 #include "Ifpack_ConfigDefs.h"
88 template<
typename key_type>
95 n_keys_ = getRecommendedHashSize(n_keys) ;
97 seed_ = (2654435761U);
102 keys_.resize(n_sets_);
103 vals_.resize(n_sets_);
105 for (
int i = 0; i < n_sets_; ++i)
107 keys_[i].resize(n_keys_);
108 vals_[i].resize(n_keys_);
111 counter_.resize(n_keys_);
112 for (
int i = 0; i < n_keys_; ++i) counter_[i] = 0;
116 inline double get(
const key_type key)
118 int hashed_key = doHash(key);
120 for (
int set_ptr = 0; set_ptr < counter_[hashed_key]; ++set_ptr)
122 if (keys_[set_ptr][hashed_key] == key)
123 return(vals_[set_ptr][hashed_key]);
130 inline void set(
const key_type key,
const double value,
131 const bool addToValue =
false)
133 int hashed_key = doHash(key);
134 int& hashed_counter = counter_[hashed_key];
136 for (
int set_ptr = 0; set_ptr < hashed_counter; ++set_ptr)
138 if (keys_[set_ptr][hashed_key] == key)
141 vals_[set_ptr][hashed_key] += value;
143 vals_[set_ptr][hashed_key] = value;
148 if (hashed_counter < n_sets_)
150 keys_[hashed_counter][hashed_key] = key;
151 vals_[hashed_counter][hashed_key] = value;
156 std::vector<key_type> new_key;
157 std::vector<double> new_val;
159 keys_.push_back(new_key);
160 vals_.push_back(new_val);
161 keys_[n_sets_].resize(n_keys_);
162 vals_[n_sets_].resize(n_keys_);
164 keys_[n_sets_][hashed_key] = key;
165 vals_[n_sets_][hashed_key] = value;
176 memset(&counter_[0], 0,
sizeof(
int) * n_keys_);
183 for (
int key = 0; key < n_keys_; ++key)
184 n_entries += counter_[key];
189 void arrayify(key_type* key_array,
double* val_array)
192 for (
int key = 0; key < n_keys_; ++key)
193 for (
int set_ptr = 0; set_ptr < counter_[key]; ++set_ptr)
195 key_array[count] = keys_[set_ptr][key];
196 val_array[count] = vals_[set_ptr][key];
207 cout <<
"n_keys = " << n_keys_ << endl;
208 cout <<
"n_sets = " << n_sets_ << endl;
211 int getRecommendedHashSize (
int n)
218 3, 7, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
219 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
220 12582917, 25165842, 50331653, 100663319, 201326611, 402653189,
221 805306457, 1610612741 } ;
229 for (i = 6 ; i < 30 ; i++)
244 inline int doHash(
const key_type key)
246 return (key % n_keys_);
252 std::vector<std::vector<double> > vals_;
253 std::vector<std::vector<key_type> > keys_;
254 std::vector<int> counter_;
void print()
Basic printing routine.
void set(const key_type key, const double value, const bool addToValue=false)
Sets an element in the hash table.
TIfpack_HashTable(const int n_keys=1031, const int n_sets=1)
constructor.
void arrayify(key_type *key_array, double *val_array)
Converts the contents in array format for both keys and values.
int getNumEntries() const
Returns the number of stored entries.
void reset()
Resets the entries of the already allocated memory. This method can be used to clean an array...