Created
March 9, 2012 16:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://github.com/kpu/kenlm/tree/master/util