52 #ifndef IFPACK_HASHTABLE_H
53 #define IFPACK_HASHTABLE_H
55 #include "Ifpack_ConfigDefs.h"
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 int 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 int 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<int> 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];
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];
204 cout <<
"n_keys = " << n_keys_ << endl;
205 cout <<
"n_sets = " << n_sets_ << endl;
208 int getRecommendedHashSize (
int n)
215 3, 7, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
216 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
217 12582917, 25165842, 50331653, 100663319, 201326611, 402653189,
218 805306457, 1610612741 } ;
226 for (i = 6 ; i < 30 ; i++)
241 inline int doHash(
const int key)
243 return (key % n_keys_);
249 std::vector<std::vector<double> > vals_;
250 std::vector<std::vector<int> > keys_;
251 std::vector<int> counter_;
Ifpack_HashTable(const int n_keys=1031, const int n_sets=1)
constructor.
void print()
Basic printing routine.
void arrayify(int *key_array, double *val_array)
Converts the contents in array format for both keys and values.
void set(const int key, const double value, const bool addToValue=false)
Sets an element in the hash table.
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...