https://github.com/alecthomas/mph
Minimal Perfect Hashing for Go
https://github.com/alecthomas/mph
Last synced: over 1 year ago
JSON representation
Minimal Perfect Hashing for Go
- Host: GitHub
- URL: https://github.com/alecthomas/mph
- Owner: alecthomas
- License: bsd-3-clause
- Created: 2013-06-15T16:07:37.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2024-09-04T18:52:14.000Z (almost 2 years ago)
- Last Synced: 2025-04-02T04:04:58.793Z (over 1 year ago)
- Language: Go
- Size: 755 KB
- Stars: 181
- Watchers: 4
- Forks: 23
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# Minimal Perfect Hashing for Go
This library provides [Minimal Perfect Hashing](http://en.wikipedia.org/wiki/Perfect_hash_function) (MPH) using the [Compress, Hash and Displace](http://cmph.sourceforge.net/papers/esa09.pdf) (CHD) algorithm.
## What is this useful for?
Primarily, extremely efficient access to potentially very large static datasets, such as geographical data, NLP data sets, etc.
On my 2012 vintage MacBook Air, a benchmark against a wikipedia index with 300K keys against a 2GB TSV dump takes about ~200ns per lookup.
## How would it be used?
Typically, the table would be used as a fast index into a (much) larger data set, with values in the table being file offsets or similar.
The tables can be serialized. Numeric values are written in little endian form.
## Example code
Building and serializing an MPH hash table (error checking omitted for clarity):
```go
b := mph.Builder()
for k, v := range data {
b.Add(k, v)
}
h, _ := b.Build()
w, _ := os.Create("data.idx")
_ := h.Write(w)
```
Deserializing the hash table and performing lookups:
```go
r, _ := os.Open("data.idx")
h, _ := mph.Read(r)
v := h.Get([]byte("some key"))
if v == nil {
// Key not found
}
```
MMAP is also indirectly supported, by deserializing from a byte slice and slicing the keys and values.
The [API documentation](http://godoc.org/github.com/alecthomas/mph) has more details.