10#ifndef TEUCHOS_HASHTABLE_H
11#define TEUCHOS_HASHTABLE_H
28 template<
class Key,
class Value>
class HashPair
59 inline const Value&
get(
const Key& key)
const ;
62 inline void put(
const Key& key,
const Value& value);
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>
109 template<
class Key,
class Value>
112 template<
class Key,
class Value>
inline
116 capacity_(
HashUtils::nextPrime(capacity)),
121 data_.resize(capacity_);
124 template<
class Key,
class Value>
inline
128 = data_[hashCode(key) % capacity_];
143 template<
class Key,
class Value>
inline
146 int index = hashCode(key) % capacity_;
151 for (
int i=0;
i<
local.length();
i++)
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_;
195 template<
class Key,
class Value>
inline
198 keys.reserve(size());
199 values.reserve(size());
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
235 h.arrayify(
keys, values);
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_];
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_;
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
311 return os << toString(
h);
Templated array class derived from the STL std::vector.
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Utilities for generating hashcodes.
Helper class for Teuchos::Hashtable, representing a single <key, value> pair.
Key key_
Templated key variable.
HashPair()
Empty constructor.
Value value_
Templated value variable.
HashPair(const Key &key, const Value &value)
Basic <key, value> constructor.
Utilities for generating hashcodes. This is not a true hash ! For all ints and types less than ints i...
Templated hashtable class.
double density() const
Return the density of the hashtable (num entries / capacity)
std::string toString() const
Write to a std::string.
void setRehashDensity(double rehashDensity)
Set the density at which to do a rehash.
const Value & get(const Key &key) const
Get the value indexed by key.
void arrayify(Array< Key > &keys, Array< Value > &values) const
Get lists of keys and values in Array form.
int size() const
Get the number of elements in the table.
void put(const Key &key, const Value &value)
Put a new (key, value) pair in the table.
double avgDegeneracy() const
Return the average degeneracy (average number of entries per hash code).
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.
void remove(const Key &key)
Remove from the table the element given by key.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...