From 57ce26c5b19f50e556c71a08894352ded62cd91e Mon Sep 17 00:00:00 2001 From: Davi Reis Date: Wed, 21 Mar 2012 22:25:38 -0300 Subject: [PATCH] Fixed bug in ranking function. --- cxxmph/mph_index.cc | 4 +++- cxxmph/mph_map.h | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cxxmph/mph_index.cc b/cxxmph/mph_index.cc index 5a6426e..9e24fbd 100644 --- a/cxxmph/mph_index.cc +++ b/cxxmph/mph_index.cc @@ -170,7 +170,9 @@ void MPHIndex::Ranking() { while (1) { if (i == ranktable_size_) break; uint32_t nbytes = size < nbytes_total ? size : nbytes_total; - for (uint32_t j = 0; j < nbytes; ++j) count += kBdzLookupIndex[g_[offset + j]]; + for (uint32_t j = 0; j < nbytes; ++j) { + count += kBdzLookupIndex[g_.data()[offset + j]]; + } ranktable[i] = count; offset += nbytes; nbytes_total -= size; diff --git a/cxxmph/mph_map.h b/cxxmph/mph_map.h index dc7134e..8018d7f 100644 --- a/cxxmph/mph_map.h +++ b/cxxmph/mph_map.h @@ -69,14 +69,14 @@ class mph_map { void erase(iterator pos); void erase(const key_type& k); pair insert(const value_type& x); - iterator find(const key_type& k) { return slow_find(k, index_.perfect_hash(k)); } - const_iterator find(const key_type& k) const { return slow_find(k, index_.perfect_hash(k)); }; + iterator find(const key_type& k) { return slow_find(k, index_.minimal_perfect_hash(k)); } + const_iterator find(const key_type& k) const { return slow_find(k, index_.minimal_perfect_hash(k)); }; typedef int32_t my_int32_t; // help macros int32_t index(const key_type& k) const; data_type& operator[](const key_type &k); const data_type& operator[](const key_type &k) const; - size_type bucket_count() const { return index_.perfect_hash_size() + slack_.bucket_count(); } + size_type bucket_count() const { return index_.minimal_perfect_hash_size() + slack_.bucket_count(); } void rehash(size_type nbuckets /*ignored*/); protected: // mimicking STL implementation @@ -105,8 +105,8 @@ class mph_map { // Experimental functions, not always faster iterator fast_find(const key_type& k); - iterator slow_find(const key_type& k, uint32_t perfect_hash); - const_iterator slow_find(const key_type& k, uint32_t perfect_hash) const; + iterator slow_find(const key_type& k, uint32_t minimal_perfect_hash); + const_iterator slow_find(const key_type& k, uint32_t minimal_perfect_hash) const; void pack(); std::vector values_; @@ -169,13 +169,13 @@ MPH_MAP_METHOD_DECL(void_type, pack)() { make_iterator_first(begin()), make_iterator_first(end()), size_); assert(success); - std::vector new_values(index_.perfect_hash_size()); + std::vector new_values(index_.minimal_perfect_hash_size()); new_values.reserve(new_values.size() * 2); - std::vector new_present(index_.perfect_hash_size(), false); + std::vector new_present(index_.minimal_perfect_hash_size(), false); new_present.reserve(new_present.size() * 2); for (iterator it = begin(), it_end = end(); it != it_end; ++it) { - size_type id = index_.perfect_hash(it->first); - assert(id < index_.perfect_hash_size()); + size_type id = index_.minimal_perfect_hash(it->first); + assert(id < index_.minimal_perfect_hash_size()); assert(id < new_values.size()); new_values[id] = *it; new_present[id] = true; @@ -212,10 +212,10 @@ MPH_MAP_METHOD_DECL(void_type, erase)(const key_type& k) { erase(it); } -MPH_MAP_METHOD_DECL(const_iterator, slow_find)(const key_type& k, uint32_t perfect_hash) const { - if (__builtin_expect(index_.perfect_hash_size(), 1)) { - if (__builtin_expect(present_[perfect_hash], true)) { - auto vit = values_.begin() + perfect_hash; +MPH_MAP_METHOD_DECL(const_iterator, slow_find)(const key_type& k, uint32_t minimal_perfect_hash) const { + if (__builtin_expect(index_.minimal_perfect_hash_size(), 1)) { + if (__builtin_expect(present_[minimal_perfect_hash], true)) { + auto vit = values_.begin() + minimal_perfect_hash; if (equal_(k, vit->first)) return make_iterator(vit); } } @@ -227,10 +227,10 @@ MPH_MAP_METHOD_DECL(const_iterator, slow_find)(const key_type& k, uint32_t perfe return end(); } -MPH_MAP_METHOD_DECL(iterator, slow_find)(const key_type& k, uint32_t perfect_hash) { - if (__builtin_expect(index_.perfect_hash_size(), 1)) { - if (__builtin_expect(present_[perfect_hash], true)) { - auto vit = values_.begin() + perfect_hash; +MPH_MAP_METHOD_DECL(iterator, slow_find)(const key_type& k, uint32_t minimal_perfect_hash) { + if (__builtin_expect(index_.minimal_perfect_hash_size(), 1)) { + if (__builtin_expect(present_[minimal_perfect_hash], true)) { + auto vit = values_.begin() + minimal_perfect_hash; if (equal_(k, vit->first)) return make_iterator(vit); } } @@ -244,7 +244,7 @@ MPH_MAP_METHOD_DECL(iterator, slow_find)(const key_type& k, uint32_t perfect_has MPH_MAP_METHOD_DECL(my_int32_t, index)(const key_type& k) const { if (index_.size() == 0) return -1; - return index_.perfect_hash(k); + return index_.minimal_perfect_hash(k); } MPH_MAP_METHOD_DECL(data_type&, operator[])(const key_type& k) {