44 #ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DEF_HPP 45 #define TPETRA_DETAILS_FIXEDHASHTABLE_DEF_HPP 47 #ifdef DOXYGEN_USE_ONLY 48 #include "Tpetra_Details_FixedHashTable_decl.hpp" 52 #include "MurmurHash3.hpp" 58 template<
typename KeyType,
typename ValueType>
59 int FixedHashTable<KeyType, ValueType>::hashFunc (
const KeyType key)
const {
60 #ifdef TPETRA_USE_MURMUR_HASH 62 MurmurHash3_x86_32 ((
void *) &key,
sizeof (KeyType), 1, (
void *) &k);
63 return (
int) (k % size_);
70 const unsigned int seed = (2654435761U);
72 #ifdef HAVE_TPETRA_DEBUG 74 size_ == 0, std::logic_error,
"Tpetra::Details::FixedHashTable::hashFunc: " 75 "size_ == 0. Please report this bug to the Tpetra developers.");
76 #endif // HAVE_TPETRA_DEBUG 78 const int intkey = (int) ((key & 0x000000007fffffffLL) +
79 ((key & 0x7fffffff80000000LL) >> 31));
80 return (
int) ((seed ^ intkey) % size_);
84 template<
typename KeyType,
typename ValueType>
86 FixedHashTable<KeyType, ValueType>::getRecommendedSize (
const int size)
101 3, 7, 13, 23, 53, 97, 193, 389, 769, 1543,
102 2237, 2423, 2617, 2797, 2999, 3167, 3359, 3539,
103 3727, 3911, 4441 , 4787 , 5119 , 5471 , 5801 , 6143 , 6521 , 6827
104 , 7177 , 7517 , 7853 , 8887 , 9587 , 10243 , 10937 , 11617 , 12289
105 , 12967 , 13649 , 14341 , 15013 , 15727
106 , 17749 , 19121 , 20479 , 21859 , 23209 , 24593 , 25939 , 27329
107 , 28669 , 30047 , 31469 , 35507 , 38231 , 40961 , 43711 , 46439
108 , 49157 , 51893 , 54617 , 57347 , 60077 , 62801 , 70583 , 75619
109 , 80669 , 85703 , 90749 , 95783 , 100823 , 105871 , 110909 , 115963
110 , 120997 , 126031 , 141157 , 151237 , 161323 , 171401 , 181499 , 191579
111 , 201653 , 211741 , 221813 , 231893 , 241979 , 252079
112 , 282311 , 302483 , 322649 , 342803 , 362969 , 383143 , 403301 , 423457
113 , 443629 , 463787 , 483953 , 504121 , 564617 , 604949 , 645313 , 685609
114 , 725939 , 766273 , 806609 , 846931 , 887261 , 927587 , 967919 , 1008239
115 , 1123477 , 1198397 , 1273289 , 1348177 , 1423067 , 1497983 , 1572869
116 , 1647761 , 1722667 , 1797581 , 1872461 , 1947359 , 2022253
117 , 2246953 , 2396759 , 2546543 , 2696363 , 2846161 , 2995973 , 3145739
118 , 3295541 , 3445357 , 3595117 , 3744941 , 3894707 , 4044503
119 , 4493921 , 4793501 , 5093089 , 5392679 , 5692279 , 5991883 , 6291469
120 , 6591059 , 6890641 , 7190243 , 7489829 , 7789447 , 8089033
121 , 8987807 , 9586981 , 10186177 , 10785371 , 11384539 , 11983729
122 , 12582917 , 13182109 , 13781291 , 14380469 , 14979667 , 15578861
123 , 16178053 , 17895707 , 19014187 , 20132683 , 21251141 , 22369661
124 , 23488103 , 24606583 , 25725083 , 26843549 , 27962027 , 29080529
125 , 30198989 , 31317469 , 32435981 , 35791397 , 38028379 , 40265327
126 , 42502283 , 44739259 , 46976221 , 49213237 , 51450131 , 53687099
127 , 55924061 , 58161041 , 60397993 , 62634959 , 64871921
128 , 71582857 , 76056727 , 80530643 , 85004567 , 89478503 , 93952427
129 , 98426347 , 102900263 , 107374217 , 111848111 , 116322053 , 120795971
130 , 125269877 , 129743807 , 143165587 , 152113427 , 161061283 , 170009141
131 , 178956983 , 187904819 , 196852693 , 205800547 , 214748383 , 223696237
132 , 232644089 , 241591943 , 250539763 , 259487603 , 268435399 };
134 int hsize = primes[220] ;
135 for (
int i = 0; i < 221; ++i) {
136 if (size <= primes[i]) {
144 template<
typename KeyType,
typename ValueType>
151 init (keys, Teuchos::as<ValueType> (0));
154 template<
typename KeyType,
typename ValueType>
157 const ValueType startingValue) :
162 init (keys, startingValue);
165 template<
typename KeyType,
typename ValueType>
176 template<
typename KeyType,
typename ValueType>
180 const ValueType startingValue)
183 using Teuchos::arcp_const_cast;
187 const size_type numKeys = keys.
size ();
188 const size_type size = getRecommendedSize (as<int> (numKeys));
189 #ifdef HAVE_TPETRA_DEBUG 191 size == 0 && numKeys != 0, std::logic_error,
192 "Tpetra::Details::FixedHashTable constructor: " 193 "getRecommendedSize(" << numKeys <<
") returned zero, " 194 "even though the number of keys " << numKeys <<
" is nonzero. " 195 "Please report this bug to the Tpetra developers.");
196 #endif // HAVE_TPETRA_DEBUG 200 size_ = as<KeyType> (size);
210 std::pair<KeyType, ValueType>* rawVal = NULL;
212 rawVal =
new std::pair<KeyType, ValueType> [numKeys];
213 val = arcp<std::pair<KeyType, ValueType> > (rawVal, 0, numKeys,
true);
215 if (rawVal != NULL) {
222 for (size_type k = 0; k < numKeys; ++k) {
223 const int hashVal = hashFunc (keys[k]);
235 for (size_type i = 0; i < size; ++i) {
244 for (size_type k = 0; k < numKeys; ++k) {
245 const KeyType key = keys[k];
246 const ValueType theVal = startingValue + as<ValueType> (k);
247 const int hashVal = hashFunc (key);
249 const size_type offset = curRowStart[hashVal];
250 const size_type curPos = ptr[hashVal] + offset;
252 val[curPos].first = key;
253 val[curPos].second = theVal;
254 ++curRowStart[hashVal];
258 ptr_ = arcp_const_cast<
const size_type> (ptr);
267 const std::pair<KeyType, ValueType>* valRaw = val.
release ();
276 template<
typename KeyType,
typename ValueType>
283 using Teuchos::arcp_const_cast;
287 const size_type numKeys = keys.
size ();
288 const size_type size = getRecommendedSize (as<int> (numKeys));
289 #ifdef HAVE_TPETRA_DEBUG 291 size == 0 && numKeys != 0, std::logic_error,
292 "Tpetra::Details::FixedHashTable constructor: " 293 "getRecommendedSize(" << numKeys <<
") returned zero, " 294 "even though the number of keys " << numKeys <<
" is nonzero. " 295 "Please report this bug to the Tpetra developers.");
296 #endif // HAVE_TPETRA_DEBUG 300 size_ = as<KeyType> (size);
310 std::pair<KeyType, ValueType>* rawVal = NULL;
312 rawVal =
new std::pair<KeyType, ValueType> [numKeys];
313 val = arcp<std::pair<KeyType, ValueType> > (rawVal, 0, numKeys,
true);
315 if (rawVal != NULL) {
322 for (size_type k = 0; k < numKeys; ++k) {
323 const int hashVal = hashFunc (keys[k]);
335 for (size_type i = 0; i < size; ++i) {
344 for (size_type k = 0; k < numKeys; ++k) {
345 const KeyType key = keys[k];
346 const ValueType theVal = vals[k];
347 const int hashVal = hashFunc (key);
349 const size_type offset = curRowStart[hashVal];
350 const size_type curPos = ptr[hashVal] + offset;
352 val[curPos].first = key;
353 val[curPos].second = theVal;
354 ++curRowStart[hashVal];
358 ptr_ = arcp_const_cast<
const size_type> (ptr);
367 const std::pair<KeyType, ValueType>* valRaw = val.
release ();
376 template<
typename KeyType,
typename ValueType>
382 rawPtr_ (obj.rawPtr_),
383 rawVal_ (obj.rawVal_)
386 template<
typename KeyType,
typename ValueType>
389 get (
const KeyType key)
const 391 const int hashVal = hashFunc (key);
392 #ifdef HAVE_TPETRA_DEBUG 394 const size_type start = ptr_[hashVal];
395 const size_type end = ptr_[hashVal+1];
396 for (size_type k = start; k < end; ++k) {
397 if (val_[k].first == key) {
398 return val_[k].second;
402 const size_type start = rawPtr_[hashVal];
403 const size_type end = rawPtr_[hashVal+1];
404 for (size_type k = start; k < end; ++k) {
405 if (rawVal_[k].first == key) {
406 return rawVal_[k].second;
409 #endif // HAVE_TPETRA_DEBUG 413 template <
typename KeyType,
typename ValueType>
415 std::ostringstream oss;
416 oss <<
"FixedHashTable<" 419 <<
"{ numKeys: " << val_.
size ()
420 <<
", tableSize: " << ptr_.
size () <<
" }";
424 template <
typename KeyType,
typename ValueType>
433 using Teuchos::rcpFromRef;
450 out <<
"FixedHashTable:" << endl;
452 OSTab tab1 (rcpFromRef (out));
456 out <<
"label: " << label << endl;
458 out <<
"Template parameters:" << endl;
460 OSTab tab2 (rcpFromRef (out));
461 out <<
"KeyType: " << TypeNameTraits<KeyType>::name () << endl
462 <<
"ValueType: " << TypeNameTraits<ValueType>::name () << endl;
465 const size_type tableSize = size_;
466 const size_type numKeys = val_.
size ();
468 out <<
"Table parameters:" << endl;
470 OSTab tab2 (rcpFromRef (out));
471 out <<
"numKeys: " << numKeys << endl
472 <<
"tableSize: " << tableSize << endl;
477 if (tableSize == 0 || numKeys == 0) {
482 OSTab tab2 (rcpFromRef (out));
483 for (size_type i = 0; i < tableSize; ++i) {
484 OSTab tab3 (rcpFromRef (out));
486 for (size_type k = ptr_[i]; k < ptr_[i+1]; ++k) {
487 out <<
"(" << val_[k].first <<
"," << val_[k].second <<
")";
488 if (k + 1 < ptr_[i+1]) {
514 #define TPETRA_DETAILS_FIXEDHASHTABLE_INSTANT_DEFAULTNODE(LO,GO) \ 515 template class FixedHashTable< GO , LO >; \ 517 #endif // TPETRA_DETAILS_FIXEDHASHTABLE_DEF_HPP Namespace Tpetra contains the class and methods constituting the Tpetra library.
virtual std::string getObjectLabel() const
basic_OSTab< char > OSTab
ValueType get(const KeyType key) const
Get the value corresponding to the given key.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::string description() const
Implementation of Teuchos::Describable.
Implementation details of Tpetra.
FixedHashTable(const ArrayView< const KeyType > &keys)
Constructor for arbitrary keys and contiguous values starting with zero.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with the given verbosity to the output stream.
TypeTo as(const TypeFrom &t)
static std::string name()