1
Fork 0
turbonss/cxxmph/mph_map.h

359 lines
12 KiB
C
Raw Normal View History

2011-12-05 20:03:10 +02:00
#ifndef __CXXMPH_MPH_MAP_H__
#define __CXXMPH_MPH_MAP_H__
2011-11-05 19:15:11 +02:00
// Implementation of the unordered associative mapping interface using a
// minimal perfect hash function.
//
// This class is about 20% to 100% slower than unordered_map (or ext/hash_map)
// and should not be used if performance is a concern. In fact, you should only
// use it for educational purposes.
//
// See http://www.strchr.com/crc32_popcnt and new Murmur3 function to try to beat stl
2011-11-05 19:15:11 +02:00
2010-11-08 22:19:44 +02:00
#include <algorithm>
#include <iostream>
#include <limits>
2011-11-10 20:44:37 +02:00
#include <unordered_map>
#include <unordered_set>
2010-06-28 22:01:18 +03:00
#include <vector>
#include <utility> // for std::pair
2012-03-14 23:26:26 +02:00
#include "mph_bits.h"
#include "mph_index.h"
#include "hollow_iterator.h"
2010-10-29 09:26:37 +03:00
namespace cxxmph {
2011-12-10 03:57:37 +02:00
using std::pair;
using std::make_pair;
2011-11-10 20:44:37 +02:00
using std::unordered_map;
2011-12-10 03:57:37 +02:00
using std::vector;
2011-05-24 03:18:24 +03:00
2010-06-28 22:01:18 +03:00
// Save on repetitive typing.
2011-05-16 02:47:42 +03:00
#define MPH_MAP_TMPL_SPEC template <class Key, class Data, class HashFcn, class EqualKey, class Alloc>
#define MPH_MAP_CLASS_SPEC mph_map<Key, Data, HashFcn, EqualKey, Alloc>
#define MPH_MAP_METHOD_DECL(r, m) MPH_MAP_TMPL_SPEC typename MPH_MAP_CLASS_SPEC::r MPH_MAP_CLASS_SPEC::m
2010-06-28 22:01:18 +03:00
2011-11-10 20:44:37 +02:00
template <class Key, class Data, class HashFcn = std::hash<Key>, class EqualKey = std::equal_to<Key>, class Alloc = std::allocator<Data> >
2011-05-16 02:47:42 +03:00
class mph_map {
2010-06-28 22:01:18 +03:00
public:
typedef Key key_type;
typedef Data data_type;
2011-12-10 03:57:37 +02:00
typedef pair<Key, Data> value_type;
2010-06-28 22:01:18 +03:00
typedef HashFcn hasher;
typedef EqualKey key_equal;
typedef typename std::vector<value_type>::pointer pointer;
typedef typename std::vector<value_type>::reference reference;
typedef typename std::vector<value_type>::const_reference const_reference;
typedef typename std::vector<value_type>::size_type size_type;
typedef typename std::vector<value_type>::difference_type difference_type;
2012-03-07 10:10:29 +02:00
typedef hollow_iterator<std::vector<value_type>> iterator;
typedef hollow_const_iterator<std::vector<value_type>> const_iterator;
2010-06-28 22:01:18 +03:00
// For making macros simpler.
typedef void void_type;
typedef bool bool_type;
2011-12-10 03:57:37 +02:00
typedef pair<iterator, bool> insert_return_type;
2010-06-28 22:01:18 +03:00
2011-05-16 02:47:42 +03:00
mph_map();
~mph_map();
2010-06-28 22:01:18 +03:00
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
size_type size() const;
bool empty() const;
void clear();
void erase(iterator pos);
void erase(const key_type& k);
2011-12-10 03:57:37 +02:00
pair<iterator, bool> 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)); };
2011-12-10 03:57:37 +02:00
typedef int32_t my_int32_t; // help macros
2011-06-14 08:24:40 +03:00
int32_t index(const key_type& k) const;
2010-06-28 22:01:18 +03:00
data_type& operator[](const key_type &k);
2011-06-14 08:24:40 +03:00
const data_type& operator[](const key_type &k) const;
2010-06-28 22:01:18 +03:00
size_type bucket_count() const { return index_.perfect_hash_size() + slack_.bucket_count(); }
void rehash(size_type nbuckets /*ignored*/);
2010-06-28 22:01:18 +03:00
2011-06-14 08:24:40 +03:00
protected: // mimicking STL implementation
EqualKey equal_;
2010-06-28 22:01:18 +03:00
private:
2010-11-05 08:40:15 +02:00
template <typename iterator>
struct iterator_first : public iterator {
iterator_first(iterator it) : iterator(it) { }
const typename iterator::value_type::first_type& operator*() {
2010-11-05 08:40:15 +02:00
return this->iterator::operator*().first;
}
};
template <typename iterator>
iterator_first<iterator> make_iterator_first(iterator it) {
return iterator_first<iterator>(it);
}
iterator make_iterator(typename std::vector<value_type>::iterator it) {
return hollow_iterator<std::vector<value_type>>(&values_, &present_, it);
}
const_iterator make_iterator(typename std::vector<value_type>::const_iterator it) const {
return hollow_const_iterator<std::vector<value_type>>(&values_, &present_, it);
2012-03-07 10:10:29 +02:00
}
// Experimental functions, not always faster
iterator fast_find(const key_type& k);
const_iterator fast_find(const key_type& k) const;
iterator slow_find(const key_type& k, uint32_t perfect_hash);
const_iterator slow_find(const key_type& k, uint32_t perfect_hash) const;
static const uint8_t kNestCollision = 3; // biggest 2 bit value
void set_nest_value(const uint32_t* h, uint8_t value) {
2012-03-14 17:40:50 +02:00
auto index = get_nest_index(h);
2012-03-14 23:26:26 +02:00
assert(get_nest_index(h) < nests_.size());
2012-03-14 17:40:50 +02:00
assert(get_nest_index(h) >> 2 < nests_.size());
assert(value < 4);
2012-03-14 23:26:26 +02:00
nests_.set(index, value);
assert(nests_[index] == value);
}
uint32_t get_nest_value(const uint32_t* h) const {
2012-03-14 23:26:26 +02:00
assert(get_nest_index(h) < nests_.size());
return nests_[get_nest_index(h)];
}
uint32_t get_nest_index(const uint32_t* h) const {
2012-03-14 23:26:26 +02:00
assert(nests_.size());
assert(nests_.size() % 2 == 0);
assert((nests_.size() & (nests_.size() - 1)) == 0);
assert((h[3] % nests_.size()) == (h[3] & (nests_.size() - 1)));
return (h[3] & (nests_.size() - 1)); // a mod 2^n == a & 2^n - 1
}
2011-05-24 03:18:24 +03:00
void pack();
2010-11-05 08:40:15 +02:00
std::vector<value_type> values_;
std::vector<bool> present_;
2012-03-14 23:26:26 +02:00
dynamic_2bitset nests_;
SimpleMPHIndex<Key, typename seeded_hash<HashFcn>::hash_function> index_;
2012-03-14 21:43:38 +02:00
// TODO(davi) optimize slack to hold 128 unique bits from hash64 as key
2011-05-24 03:18:24 +03:00
typedef unordered_map<Key, uint32_t, HashFcn, EqualKey, Alloc> slack_type;
2010-11-05 08:40:15 +02:00
slack_type slack_;
size_type size_;
2012-03-14 23:26:26 +02:00
mutable uint64_t fast_;
mutable uint64_t fast_taken_;
2012-03-14 23:26:26 +02:00
mutable uint64_t slow_;
mutable uint64_t very_slow_;
2010-06-28 22:01:18 +03:00
};
2011-05-16 02:47:42 +03:00
MPH_MAP_TMPL_SPEC
bool operator==(const MPH_MAP_CLASS_SPEC& lhs, const MPH_MAP_CLASS_SPEC& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
2010-06-28 22:01:18 +03:00
}
MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::mph_map() : size_(0) {
clear();
2011-05-24 03:18:24 +03:00
pack();
2010-06-28 22:01:18 +03:00
}
2011-05-16 02:47:42 +03:00
MPH_MAP_TMPL_SPEC MPH_MAP_CLASS_SPEC::~mph_map() {
// fprintf(stderr, "Fast taken: %d Fast: %d Slow %d very_slow %d ratio %f\n", fast_taken_, fast_, slow_, very_slow_, fast_*1.0/slow_);
2010-06-28 22:01:18 +03:00
}
2011-05-16 02:47:42 +03:00
MPH_MAP_METHOD_DECL(insert_return_type, insert)(const value_type& x) {
auto it = find(x.first);
auto it_end = end();
if (it != it_end) return make_pair(it, false);
bool should_pack = false;
2012-03-07 10:10:29 +02:00
if (values_.capacity() == values_.size() && values_.size() > 256) {
should_pack = true;
}
2010-06-28 22:01:18 +03:00
values_.push_back(x);
present_.push_back(true);
uint32_t h[4];
index_.hash_vector(x.first, h);
set_nest_value(h, kNestCollision);
++size_;
2011-12-10 03:57:37 +02:00
slack_.insert(make_pair(x.first, values_.size() - 1));
2012-03-07 10:10:29 +02:00
if (should_pack) pack();
2010-06-28 22:01:18 +03:00
it = find(x.first);
slow_ = 0;
very_slow_ = 0;
fast_ = 0;
fast_taken_ = 0;
2011-12-10 03:57:37 +02:00
return make_pair(it, true);
2010-06-28 22:01:18 +03:00
}
2011-05-24 03:18:24 +03:00
MPH_MAP_METHOD_DECL(void_type, pack)() {
// fprintf(stderr, "Paki %d values\n", values_.size());
2010-06-28 22:01:18 +03:00
if (values_.empty()) return;
assert(std::unordered_set<key_type>(make_iterator_first(begin()), make_iterator_first(end())).size() == size());
bool success = index_.Reset(
make_iterator_first(begin()),
make_iterator_first(end()), size_);
2010-11-09 02:02:18 +02:00
assert(success);
std::vector<value_type> new_values(index_.perfect_hash_size());
new_values.reserve(new_values.size() * 2);
std::vector<bool> new_present(index_.perfect_hash_size(), false);
new_present.reserve(new_present.size() * 2);
auto new_nests_size = nextpoweroftwo(ceil(new_values.size())*10000 + 1);
2012-03-14 23:26:26 +02:00
dynamic_2bitset(new_nests_size, true /* fill with 1s */).swap(nests_);
vector<bool> used_nests(nests_.size());
uint32_t collisions = 0;
for (iterator it = begin(), it_end = end(); it != it_end; ++it) {
size_type id = index_.perfect_hash(it->first);
2011-05-16 05:04:30 +03:00
assert(id < new_values.size());
2010-11-06 13:14:07 +02:00
new_values[id] = *it;
new_present[id] = true;
uint32_t h[4];
index_.hash_vector(it->first, h);
// fprintf(stderr, "Nest index: %d\n", get_nest_index(h));
assert(used_nests.size() > get_nest_index(h));
if (used_nests[get_nest_index(h)]) {
set_nest_value(h, kNestCollision);
2012-03-14 17:40:50 +02:00
assert(get_nest_value(h) == kNestCollision);
2012-03-14 23:26:26 +02:00
// fprintf(stderr, "Collision at nest index %d among %d positions\n", get_nest_index(h), nests_.size());
++collisions;
} else {
set_nest_value(h, index_.cuckoo_nest(h));
2012-03-14 17:40:50 +02:00
assert(get_nest_value(h) == index_.cuckoo_nest(h));
assert(index_.perfect_hash(it->first) == index_.cuckoo_hash(h, get_nest_value(h)));
used_nests[get_nest_index(h)] = true;
}
2010-06-28 22:01:18 +03:00
}
2012-03-14 23:26:26 +02:00
// fprintf(stderr, "Collision ratio: %f\n", collisions*1.0/size());
2010-06-28 22:01:18 +03:00
values_.swap(new_values);
present_.swap(new_present);
slack_type().swap(slack_);
int32_t fast = 0;
int32_t slow= 0;
for (iterator it = begin(), it_end = end(); it != it_end; ++it) {
uint32_t h[4];
index_.hash_vector(it->first, h);
if (get_nest_value(h) == kNestCollision) ++slow;
else {
++fast;
auto cit = values_.begin() + index_.cuckoo_hash(h, get_nest_value(h));
assert(index_.perfect_hash(it->first) == cit - values_.begin());
assert(equal_(it->first, cit->first));
}
}
// fprintf(stderr, "Predicted fast: %d slow %d\n", fast, slow);
2010-06-28 22:01:18 +03:00
}
MPH_MAP_METHOD_DECL(iterator, begin)() { return make_iterator(values_.begin()); }
MPH_MAP_METHOD_DECL(iterator, end)() { return make_iterator(values_.end()); }
MPH_MAP_METHOD_DECL(const_iterator, begin)() const { return make_iterator(values_.begin()); }
MPH_MAP_METHOD_DECL(const_iterator, end)() const { return make_iterator(values_.end()); }
MPH_MAP_METHOD_DECL(bool_type, empty)() const { return size_ == 0; }
MPH_MAP_METHOD_DECL(size_type, size)() const { return size_; }
2010-06-28 22:01:18 +03:00
2011-05-16 02:47:42 +03:00
MPH_MAP_METHOD_DECL(void_type, clear)() {
2010-06-28 22:01:18 +03:00
values_.clear();
present_.clear();
2010-06-28 22:01:18 +03:00
slack_.clear();
index_.clear();
dynamic_2bitset(8, true /* fill with 1s */).swap(nests_);
size_ = 0;
2010-06-28 22:01:18 +03:00
}
2011-05-16 02:47:42 +03:00
MPH_MAP_METHOD_DECL(void_type, erase)(iterator pos) {
present_[pos - begin] = false;
uint32_t h[4];
index_.hash_vector(pos->first, &h);
nests_[get_nest_index(h)] = kNestCollision;
*pos = value_type();
--size_;
2010-06-28 22:01:18 +03:00
}
2011-05-16 02:47:42 +03:00
MPH_MAP_METHOD_DECL(void_type, erase)(const key_type& k) {
2010-06-28 22:01:18 +03:00
iterator it = find(k);
if (it == end()) return;
erase(it);
}
MPH_MAP_METHOD_DECL(const_iterator, fast_find)(const key_type& k) const {
uint32_t h[4];
index_.hash_vector(k, h);
auto nest = get_nest_value(h);
if (__builtin_expect(nest != kNestCollision, 1)) {
++fast_taken_;
auto vit = values_.begin() + index_.cuckoo_hash(h, nest);
// do not hold for unknown keys
assert(values_.size() != index_.perfect_hash_size() || equal_(k, vit->first));
2012-03-14 23:26:26 +02:00
if (equal_(k, vit->first)) {
++fast_;
return make_iterator(vit);
}
2010-06-28 22:01:18 +03:00
}
nest = index_.cuckoo_nest(h);
2012-03-14 23:26:26 +02:00
++slow_;
return slow_find(k, index_.cuckoo_hash(h, nest));
}
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;
if (equal_(k, vit->first)) return make_iterator(vit);
}
}
if (__builtin_expect(!slack_.empty(), 0)) {
++very_slow_;
auto sit = slack_.find(k);
if (sit != slack_.end()) return make_iterator(values_.begin() + sit->second);
}
2010-06-28 22:01:18 +03:00
return end();
}
2011-06-14 08:24:40 +03:00
MPH_MAP_METHOD_DECL(iterator, fast_find)(const key_type& k) {
uint32_t h[4];
index_.hash_vector(k, h);
auto nest = get_nest_value(h);
if (__builtin_expect(nest != kNestCollision, 1)) {
++fast_taken_;
auto vit = values_.begin() + index_.cuckoo_hash(h, nest);
assert(values_.size() != index_.perfect_hash_size() || equal_(k, vit->first));
2012-03-14 17:40:50 +02:00
if (equal_(k, vit->first)) {
2012-03-14 23:26:26 +02:00
++fast_;
return make_iterator(vit);
2012-03-14 17:40:50 +02:00
}
2010-06-28 22:01:18 +03:00
}
nest = index_.cuckoo_nest(h);
2012-03-14 23:26:26 +02:00
++slow_;
return slow_find(k, index_.cuckoo_hash(h, nest));
}
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;
if (equal_(k, vit->first)) return make_iterator(vit);
}
}
if (__builtin_expect(!slack_.empty(), 0)) {
++very_slow_;
auto sit = slack_.find(k);
if (sit != slack_.end()) return make_iterator(values_.begin() + sit->second);
}
2010-06-28 22:01:18 +03:00
return end();
}
2011-06-14 08:24:40 +03:00
MPH_MAP_METHOD_DECL(my_int32_t, index)(const key_type& k) const {
if (index_.size() == 0) return -1;
2012-03-07 10:10:29 +02:00
return index_.perfect_hash(k);
2011-06-14 08:24:40 +03:00
}
2011-05-16 02:47:42 +03:00
MPH_MAP_METHOD_DECL(data_type&, operator[])(const key_type& k) {
2011-12-10 03:57:37 +02:00
return insert(make_pair(k, data_type())).first->second;
2010-06-28 22:01:18 +03:00
}
MPH_MAP_METHOD_DECL(void_type, rehash)(size_type nbuckets) {
pack();
vector<value_type>(values_.begin(), values_.end()).swap(values_);
vector<bool>(present_.begin(), present_.end()).swap(present_);
slack_type().swap(slack_);
}
2010-10-29 09:26:37 +03:00
} // namespace cxxmph
2011-12-05 20:03:10 +02:00
#endif // __CXXMPH_MPH_MAP_H__