summaryrefslogtreecommitdiff
path: root/utils/perfect_hash.cc
blob: 706e2741ed66665c9eea1ca1d0b247192b9c8009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "config.h"

#ifdef HAVE_CMPH

#include "perfect_hash.h"

#include <cstdio>
#include <iostream>

using namespace std;

PerfectHashFunction::~PerfectHashFunction() {
  cmph_destroy(mphf_);
}

PerfectHashFunction::PerfectHashFunction(const string& fname) {
  FILE* f = fopen(fname.c_str(), "r");
  if (!f) {
    cerr << "Failed to open file " << fname << " for reading: cannot load hash function.\n";
    abort();
  }
  mphf_ = cmph_load(f);
  if (!mphf_) {
    cerr << "cmph_load failed on " << fname << "!\n";
    abort();
  }
}

size_t PerfectHashFunction::operator()(const string& key) const {
  return cmph_search(mphf_, &key[0], key.size());
}

size_t PerfectHashFunction::number_of_keys() const {
  return cmph_size(mphf_);
}

#endif