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 class Ifpack_HashTable
00077 {
00078 public:
00080 Ifpack_HashTable(const int n_keys = 1031, const int n_sets = 1)
00081 {
00082 n_keys_ = n_keys;
00083 n_sets_ = n_sets;
00084 seed_ = (2654435761U);
00085
00086 keys_.reserve(50);
00087 vals_.reserve(50);
00088
00089 keys_.resize(n_sets_);
00090 vals_.resize(n_sets_);
00091
00092 for (int i = 0; i < n_sets_; ++i)
00093 {
00094 keys_[i].resize(n_keys_);
00095 vals_[i].resize(n_keys_);
00096 }
00097
00098 counter_.resize(n_keys_);
00099 for (int i = 0; i < n_keys_; ++i) counter_[i] = 0;
00100 }
00101
00103 inline double get(const int key)
00104 {
00105 int hashed_key = doHash(key);
00106
00107 for (int set_ptr = 0; set_ptr < counter_[hashed_key]; ++set_ptr)
00108 {
00109 if (keys_[set_ptr][hashed_key] == key)
00110 return(vals_[set_ptr][hashed_key]);
00111 }
00112
00113 return(0.0);
00114 }
00115
00117 inline void set(const int key, const double value,
00118 const bool addToValue = false)
00119 {
00120 int hashed_key = doHash(key);
00121 int& hashed_counter = counter_[hashed_key];
00122
00123 for (int set_ptr = 0; set_ptr < hashed_counter; ++set_ptr)
00124 {
00125 if (keys_[set_ptr][hashed_key] == key)
00126 {
00127 if (addToValue)
00128 vals_[set_ptr][hashed_key] += value;
00129 else
00130 vals_[set_ptr][hashed_key] = value;
00131 return;
00132 }
00133 }
00134
00135 if (hashed_counter < n_sets_)
00136 {
00137 keys_[hashed_counter][hashed_key] = key;
00138 vals_[hashed_counter][hashed_key] = value;
00139 ++hashed_counter;
00140 return;
00141 }
00142
00143 std::vector<int> new_key;
00144 std::vector<double> new_val;
00145
00146 keys_.push_back(new_key);
00147 vals_.push_back(new_val);
00148 keys_[n_sets_].resize(n_keys_);
00149 vals_[n_sets_].resize(n_keys_);
00150
00151 keys_[n_sets_][hashed_key] = key;
00152 vals_[n_sets_][hashed_key] = value;
00153 ++hashed_counter;
00154 ++n_sets_;
00155 }
00156
00161 inline void reset()
00162 {
00163 memset(&counter_[0], 0, sizeof(int) * n_keys_);
00164 }
00165
00167 inline int getNumEntries() const
00168 {
00169 int n_entries = 0;
00170 for (int key = 0; key < n_keys_; ++key)
00171 n_entries += counter_[key];
00172 return(n_entries);
00173 }
00174
00176 void arrayify(int* key_array, double* val_array)
00177 {
00178 int count = 0;
00179 for (int key = 0; key < n_keys_; ++key)
00180 for (int set_ptr = 0; set_ptr < counter_[key]; ++set_ptr)
00181 {
00182 key_array[count] = keys_[set_ptr][key];
00183 val_array[count] = vals_[set_ptr][key];
00184 ++count;
00185 }
00186 }
00187
00189 void print()
00190 {
00191 cout << "n_keys = " << n_keys_ << endl;
00192 cout << "n_sets = " << n_sets_ << endl;
00193 }
00194
00195 private:
00197 inline int doHash(const int key)
00198 {
00199 return (key % n_keys_);
00200
00201 }
00202
00203 int n_keys_;
00204 int n_sets_;
00205 std::vector<std::vector<double> > vals_;
00206 std::vector<std::vector<int> > keys_;
00207 std::vector<int> counter_;
00208 unsigned int seed_;
00209 };
00210 #endif