00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef IFPACK_HASHTABLE_H
00041 #define IFPACK_HASHTABLE_H
00042
00043 #include "Ifpack_ConfigDefs.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 class Ifpack_HashTable
00078 {
00079 public:
00081 Ifpack_HashTable(const int n_keys = 1031, const int n_sets = 1)
00082 {
00083 n_keys_ = getRecommendedHashSize(n_keys) ;
00084 n_sets_ = n_sets;
00085 seed_ = (2654435761U);
00086
00087 keys_.reserve(50);
00088 vals_.reserve(50);
00089
00090 keys_.resize(n_sets_);
00091 vals_.resize(n_sets_);
00092
00093 for (int i = 0; i < n_sets_; ++i)
00094 {
00095 keys_[i].resize(n_keys_);
00096 vals_[i].resize(n_keys_);
00097 }
00098
00099 counter_.resize(n_keys_);
00100 for (int i = 0; i < n_keys_; ++i) counter_[i] = 0;
00101 }
00102
00104 inline double get(const int key)
00105 {
00106 int hashed_key = doHash(key);
00107
00108 for (int set_ptr = 0; set_ptr < counter_[hashed_key]; ++set_ptr)
00109 {
00110 if (keys_[set_ptr][hashed_key] == key)
00111 return(vals_[set_ptr][hashed_key]);
00112 }
00113
00114 return(0.0);
00115 }
00116
00118 inline void set(const int key, const double value,
00119 const bool addToValue = false)
00120 {
00121 int hashed_key = doHash(key);
00122 int& hashed_counter = counter_[hashed_key];
00123
00124 for (int set_ptr = 0; set_ptr < hashed_counter; ++set_ptr)
00125 {
00126 if (keys_[set_ptr][hashed_key] == key)
00127 {
00128 if (addToValue)
00129 vals_[set_ptr][hashed_key] += value;
00130 else
00131 vals_[set_ptr][hashed_key] = value;
00132 return;
00133 }
00134 }
00135
00136 if (hashed_counter < n_sets_)
00137 {
00138 keys_[hashed_counter][hashed_key] = key;
00139 vals_[hashed_counter][hashed_key] = value;
00140 ++hashed_counter;
00141 return;
00142 }
00143
00144 std::vector<int> new_key;
00145 std::vector<double> new_val;
00146
00147 keys_.push_back(new_key);
00148 vals_.push_back(new_val);
00149 keys_[n_sets_].resize(n_keys_);
00150 vals_[n_sets_].resize(n_keys_);
00151
00152 keys_[n_sets_][hashed_key] = key;
00153 vals_[n_sets_][hashed_key] = value;
00154 ++hashed_counter;
00155 ++n_sets_;
00156 }
00157
00162 inline void reset()
00163 {
00164 memset(&counter_[0], 0, sizeof(int) * n_keys_);
00165 }
00166
00168 inline int getNumEntries() const
00169 {
00170 int n_entries = 0;
00171 for (int key = 0; key < n_keys_; ++key)
00172 n_entries += counter_[key];
00173 return(n_entries);
00174 }
00175
00177 void arrayify(int* key_array, double* val_array)
00178 {
00179 int count = 0;
00180 for (int key = 0; key < n_keys_; ++key)
00181 for (int set_ptr = 0; set_ptr < counter_[key]; ++set_ptr)
00182 {
00183 key_array[count] = keys_[set_ptr][key];
00184 val_array[count] = vals_[set_ptr][key];
00185 ++count;
00186 }
00187 }
00188
00190 void print()
00191 {
00192 cout << "n_keys = " << n_keys_ << endl;
00193 cout << "n_sets = " << n_sets_ << endl;
00194 }
00195
00196 int getRecommendedHashSize (int n)
00197 {
00198
00199
00200
00201
00202 int primes[] = {
00203 3, 7, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
00204 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
00205 12582917, 25165842, 50331653, 100663319, 201326611, 402653189,
00206 805306457, 1610612741 } ;
00207 int i, hsize ;
00208
00209
00210
00211
00212
00213 hsize = primes[29] ;
00214 for (i = 6 ; i < 30 ; i++)
00215 {
00216 if (n <= primes[i])
00217 {
00218
00219 hsize = primes[i] ;
00220 break ;
00221 }
00222 }
00223
00224 return hsize ;
00225 }
00226
00227 private:
00229 inline int doHash(const int key)
00230 {
00231 return (key % n_keys_);
00232
00233 }
00234
00235 int n_keys_;
00236 int n_sets_;
00237 std::vector<std::vector<double> > vals_;
00238 std::vector<std::vector<int> > keys_;
00239 std::vector<int> counter_;
00240 unsigned int seed_;
00241 };
00242 #endif