52 #ifndef IFPACK_HASHTABLE_H
53 #define IFPACK_HASHTABLE_H
55 #if defined(Ifpack_SHOW_DEPRECATED_WARNINGS)
57 #warning "The Ifpack package is deprecated"
61 #include "Ifpack_ConfigDefs.h"
94 template<
typename key_type>
101 n_keys_ = getRecommendedHashSize(n_keys) ;
103 seed_ = (2654435761U);
108 keys_.resize(n_sets_);
109 vals_.resize(n_sets_);
111 for (
int i = 0; i < n_sets_; ++i)
113 keys_[i].resize(n_keys_);
114 vals_[i].resize(n_keys_);
117 counter_.resize(n_keys_);
118 for (
int i = 0; i < n_keys_; ++i) counter_[i] = 0;
122 inline double get(
const key_type key)
124 int hashed_key = doHash(key);
126 for (
int set_ptr = 0; set_ptr < counter_[hashed_key]; ++set_ptr)
128 if (keys_[set_ptr][hashed_key] == key)
129 return(vals_[set_ptr][hashed_key]);
136 inline void set(
const key_type key,
const double value,
137 const bool addToValue =
false)
139 int hashed_key = doHash(key);
140 int& hashed_counter = counter_[hashed_key];
142 for (
int set_ptr = 0; set_ptr < hashed_counter; ++set_ptr)
144 if (keys_[set_ptr][hashed_key] == key)
147 vals_[set_ptr][hashed_key] += value;
149 vals_[set_ptr][hashed_key] = value;
154 if (hashed_counter < n_sets_)
156 keys_[hashed_counter][hashed_key] = key;
157 vals_[hashed_counter][hashed_key] = value;
162 std::vector<key_type> new_key;
163 std::vector<double> new_val;
165 keys_.push_back(new_key);
166 vals_.push_back(new_val);
167 keys_[n_sets_].resize(n_keys_);
168 vals_[n_sets_].resize(n_keys_);
170 keys_[n_sets_][hashed_key] = key;
171 vals_[n_sets_][hashed_key] = value;
182 memset(&counter_[0], 0,
sizeof(
int) * n_keys_);
189 for (
int key = 0; key < n_keys_; ++key)
190 n_entries += counter_[key];
195 void arrayify(key_type* key_array,
double* val_array)
198 for (
int key = 0; key < n_keys_; ++key)
199 for (
int set_ptr = 0; set_ptr < counter_[key]; ++set_ptr)
201 key_array[count] = keys_[set_ptr][key];
202 val_array[count] = vals_[set_ptr][key];
213 cout <<
"n_keys = " << n_keys_ << endl;
214 cout <<
"n_sets = " << n_sets_ << endl;
217 int getRecommendedHashSize (
int n)
224 3, 7, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
225 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
226 12582917, 25165842, 50331653, 100663319, 201326611, 402653189,
227 805306457, 1610612741 } ;
235 for (i = 6 ; i < 30 ; i++)
250 inline int doHash(
const key_type key)
252 return (key % n_keys_);
258 std::vector<std::vector<double> > vals_;
259 std::vector<std::vector<key_type> > keys_;
260 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...