10 #ifndef TEUCHOS_HASHTABLE_H
11 #define TEUCHOS_HASHTABLE_H
28 template<
class Key,
class Value>
class HashPair
34 inline HashPair(
const Key& key,
const Value& value)
53 inline Hashtable(
int capacity=101,
double rehashDensity = 0.8);
59 inline const Value&
get(
const Key& key)
const ;
62 inline void put(
const Key& key,
const Value& value);
65 inline void remove(
const Key& key);
68 inline int size()
const {
return count_;}
77 inline double density()
const {
return ((
double)count_)/((double) capacity_);}
83 inline std::string
toString()
const ;
88 inline int nextPrime(
int newCap)
const ;
89 inline void accumulateAvgFill(
int n)
const ;
95 mutable Value mostRecentValue_;
96 mutable Key mostRecentKey_;
98 mutable size_t nHits_;
99 mutable double avgDegeneracy_;
100 double rehashDensity_;
103 template<
class Key,
class Value>
104 std::string toString(
const Hashtable<Key, Value>& h);
109 template<
class Key,
class Value>
110 std::ostream& operator<<(std::ostream& os, const Hashtable<Key, Value>& h);
112 template<
class Key,
class Value>
inline
116 capacity_(
HashUtils::nextPrime(capacity)),
119 rehashDensity_(rehashDensity)
121 data_.resize(capacity_);
124 template<
class Key,
class Value>
inline
128 = data_[hashCode(key) % capacity_];
130 for (
int i=0; i<candidates.
length(); i++)
143 template<
class Key,
class Value>
inline
146 int index = hashCode(key) % capacity_;
151 for (
int i=0; i<local.
length(); i++)
153 if (local[i].key_ == key)
155 local[i].value_ = value;
164 if ((
double) count_ > rehashDensity_ * (double) capacity_)
166 capacity_ = HashUtils::nextPrime(capacity_+1);
169 index = hashCode(key) % capacity_;
177 template<
class Key,
class Value>
inline
182 for (
int i=0; i<data_.length(); i++)
184 for (
int j=0; j<data_[i].length(); j++)
186 int newIndex = hashCode(data_[i][j].key_) % capacity_;
187 tmp[newIndex].append(data_[i][j]);
195 template<
class Key,
class Value>
inline
201 for (
int i=0; i<data_.length(); i++)
203 for (
int j=0; j<data_[i].length(); j++)
205 keys.
append(data_[i][j].key_);
206 values.
append(data_[i][j].value_);
211 template<
class Key,
class Value>
inline
216 arrayify(keys, values);
218 std::string rtn =
"[";
219 for (
int i=0; i<keys.
length(); i++)
221 rtn +=
"{" + Teuchos::toString(keys[i]) +
", " + Teuchos::toString(values[i])
223 if (i < keys.
length()-1) rtn +=
", ";
230 template<
class Key,
class Value>
inline
237 std::string rtn =
"[";
238 for (
int i=0; i<keys.
length(); i++)
240 rtn +=
"{" + Teuchos::toString(keys[i]) +
", " + Teuchos::toString(values[i])
242 if (i < keys.
length()-1) rtn +=
", ";
249 template<
class Key,
class Value>
inline
254 "Hashtable<Key, Value>::get: key "
255 << Teuchos::toString(key)
256 <<
" not found in Hashtable"
260 = data_[hashCode(key) % capacity_];
262 accumulateAvgFill(candidates.
length());
264 for (
int i=0; i<candidates.
length(); i++)
272 return mostRecentValue_;
276 template<
class Key,
class Value>
inline
281 "Hashtable<Key, Value>::remove: key "
282 << Teuchos::toString(key)
283 <<
" not found in Hashtable"
287 int h = hashCode(key) % capacity_;
290 for (
int i=0; i<candidates.
length(); i++)
301 template<
class Key,
class Value>
inline
304 avgDegeneracy_ = ((double) nHits_)/(nHits_ + 1.0) * avgDegeneracy_ + ((double) n)/(nHits_ + 1.0);
308 template<
class Key,
class Value>
inline
309 std::ostream& operator<<(std::ostream& os, const Hashtable<Key, Value>& h)
311 return os << toString(h);
317 #endif // TEUCHOS_HASHTABLE_H
void remove(int i)
Remove the i-th element from the array, with optional boundschecking.
Templated hashtable class.
void reserve(size_type n)
Utilities for generating hashcodes. This is not a true hash ! For all ints and types less than ints i...
Array< T > & append(const T &x)
Add a new entry at the end of the array.
void arrayify(Array< Key > &keys, Array< Value > &values) const
Get lists of keys and values in Array form.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
HashPair()
Empty constructor.
Value value_
Templated value variable.
std::string toString() const
Write to a std::string.
double avgDegeneracy() const
Return the average degeneracy (average number of entries per hash code).
double density() const
Return the density of the hashtable (num entries / capacity)
bool containsKey(const Key &key) const
Check for the presence of a key.
Hashtable(int capacity=101, double rehashDensity=0.8)
Create an empty Hashtable.
Templated array class derived from the STL std::vector.
void put(const Key &key, const Value &value)
Put a new (key, value) pair in the table.
int length() const
Return number of elements in the array.
void setRehashDensity(double rehashDensity)
Set the density at which to do a rehash.
Helper class for Teuchos::Hashtable, representing a single <key, value> pair.
Utilities for generating hashcodes.
HashPair(const Key &key, const Value &value)
Basic <key, value> constructor.
const Value & get(const Key &key) const
Get the value indexed by key.
int size() const
Get the number of elements in the table.
void remove(const Key &key)
Remove from the table the element given by key.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...
Key key_
Templated key variable.