Skip to content

Instantly share code, notes, and snippets.

@jhclark jhclark/dumb.cc

Created Mar 9, 2012
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

jhclark commented Mar 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.