Skip to content

Instantly share code, notes, and snippets.

@jhclark
Created March 9, 2012 16:38
Show Gist options
  • Select an option

  • Save jhclark/2007402 to your computer and use it in GitHub Desktop.

Select an option

Save jhclark/2007402 to your computer and use it in GitHub Desktop.

Revisions

  1. jhclark created this gist Mar 9, 2012.
    69 changes: 69 additions & 0 deletions dumb.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    // 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;
    }