diff options
author | Chris Dyer <cdyer@cs.cmu.edu> | 2011-01-18 15:55:40 -0500 |
---|---|---|
committer | Chris Dyer <cdyer@cs.cmu.edu> | 2011-01-18 15:55:40 -0500 |
commit | bee6a3c3f6c54cf7449229488c6124dddc7e2f31 (patch) | |
tree | f407d57998b648d9341990d4a4de974b5104ed97 /klm/util/probing_hash_table.hh | |
parent | 10715e9606500b34c87df9de8a39f8a7d6ecd04b (diff) |
new version of klm
Diffstat (limited to 'klm/util/probing_hash_table.hh')
-rw-r--r-- | klm/util/probing_hash_table.hh | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/klm/util/probing_hash_table.hh b/klm/util/probing_hash_table.hh index c3529a7e..7b5cdc22 100644 --- a/klm/util/probing_hash_table.hh +++ b/klm/util/probing_hash_table.hh @@ -1,6 +1,8 @@ #ifndef UTIL_PROBING_HASH_TABLE__ #define UTIL_PROBING_HASH_TABLE__ +#include "util/exception.hh" + #include <algorithm> #include <cstddef> #include <functional> @@ -9,6 +11,13 @@ namespace util { +/* Thrown when table grows too large */ +class ProbingSizeException : public Exception { + public: + ProbingSizeException() throw() {} + ~ProbingSizeException() throw() {} +}; + /* Non-standard hash table * Buckets must be set at the beginning and must be greater than maximum number * of elements, else an infinite loop happens. @@ -33,9 +42,9 @@ template <class PackingT, class HashT, class EqualT = std::equal_to<typename Pac } // Must be assigned to later. - ProbingHashTable() + ProbingHashTable() : entries_(0) #ifdef DEBUG - : initialized_(false), entries_(0) + , initialized_(false) #endif {} @@ -45,17 +54,18 @@ template <class PackingT, class HashT, class EqualT = std::equal_to<typename Pac end_(begin_ + (allocated / Packing::kBytes)), invalid_(invalid), hash_(hash_func), - equal_(equal_func) + equal_(equal_func), + entries_(0) #ifdef DEBUG , initialized_(true), - entries_(0) #endif {} template <class T> void Insert(const T &t) { + if (++entries_ >= buckets_) + UTIL_THROW(ProbingSizeException, "Hash table with " << buckets_ << " buckets is full."); #ifdef DEBUG assert(initialized_); - assert(++entries_ < buckets_); #endif for (MutableIterator i(begin_ + (hash_(t.GetKey()) % buckets_));;) { if (equal_(i->GetKey(), invalid_)) { *i = t; return; } @@ -86,9 +96,9 @@ template <class PackingT, class HashT, class EqualT = std::equal_to<typename Pac Key invalid_; Hash hash_; Equal equal_; + std::size_t entries_; #ifdef DEBUG bool initialized_; - std::size_t entries_; #endif }; |