Skip to content

Instantly share code, notes, and snippets.

@jhclark
Created March 9, 2012 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhclark/2007402 to your computer and use it in GitHub Desktop.
Save jhclark/2007402 to your computer and use it in GitHub Desktop.
Example of using KenLM's probing hash map for one's own evil purposes
// building:
// g++ -I$HOME/prefix/include/ -Iklm -c dumb.cc
// g++ -lz klm/util/file.o klm/util/mmap.o klm/util/exception.o dumb.o -o dumb
#include <stdint.h>
#include <iostream>
using namespace std;
#include <boost/functional/hash.hpp>
#include "util/probing_hash_table.hh"
#include "util/scoped.hh"
#include "util/file.hh"
#include "util/mmap.hh"
struct Entry {
unsigned char key;
typedef unsigned char Key;
unsigned char GetKey() const {
return key;
}
uint64_t GetValue() const {
return value;
}
uint64_t value;
};
typedef util::ProbingHashTable<Entry, boost::hash<unsigned char> > Table;
int main(int argc, char** argv) {
char* filename = "x.bin";
{
float load_factor = 1.5;
int num_elements = 10;
size_t bytes = Table::Size(num_elements, load_factor);
util::scoped_fd file;
util::scoped_mmap mem(MapZeroedWrite(filename, bytes, file), bytes);
Table table(mem.get(), mem.size());
Entry to_ins;
to_ins.key = 3;
to_ins.value = 328920;
cerr << "Inserting: " << (int) to_ins.key << " " << to_ins.value << endl;
table.Insert(to_ins);
cerr << "Closing " << filename << endl;
}
{
cerr << "Opening " << filename << endl;
util::scoped_fd file(util::OpenReadOrThrow(filename));
uint64_t size = util::SizeFile(file.get());
util::scoped_memory mem;
MapRead(util::POPULATE_OR_READ, file.get(), 0, size, mem);
Table table(mem.get(), mem.size());
const Entry *i = NULL;
table.Find(3, i);
cerr << "Foudn: " << (int) i->key << " " << i->value << endl;
}
return 0;
}
@jhclark
Copy link
Author

jhclark commented Mar 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment