Skip to main content

Tries with byte sequences as keys for Python and Rust

Project description

Byte tries in Rust with Python bindings

Installation

Install from PyPI (only built for Python 3.10+ on Linux)

pip install byte-prefix-tree

Manual installation

Make sure you have Rust installed, then do the following:

git clone https://github.com/bastiscode/byte-trie.git
cd byte-trie
pip install maturin[patchelf]
maturin develop --release

Usage

Currently implemented tries:

  • Patricia trie
  • Adaptive radix trie

Key needs to be a bytes object, value can be anything. Make sure that the key never contains a null/zero byte, as it is used as a terminator internally. For utf8-encoded text this usually is the case, but for other types of data you might need to encode it in a way that ensures this.

from byte_trie import PatriciaTrie, AdaptiveRadixTrie

pt = PatriciaTrie()

# add key-value pairs
pt.insert(b"hello", 1)
pt.insert(b"world", 2)

# delete key
print(pt.delete(b"hello"))  # 1

# check for keys and prefixes
print(pt.contains(b"hello"))  # False
print(pt.contains(b"world"))  # True
print(pt.contains(b"wor"))  # False
print(pt.contains_prefix(b"hel"))  # False
print(pt.contains_prefix(b"wor"))  # True

# get values
print(pt.get(b"hello"))  # None
print(pt.get(b"world"))  # 2

# overwrite
print(pt.insert(b"world", 3))  # 2
print(pt.get(b"world"))  # 3

# continuations for prefix
print(pt.continuations(b"wo"))  # [(b'world', 3)]

# prefixes of some key, returns list of (prefix length, value) tuples
key = b"world cup"
prefix_matches = pt.prefix_matches(key)
print(prefix_matches)  # [(5, 3)]
print(
    [(key[:length].decode(), value) for length, value in prefix_matches]
)  # [('world', 3)]

# same for ART
art = AdaptiveRadixTrie()

art.insert(b"hello", 1)
art.insert(b"world", 2)

print(art.delete(b"hello"))  # 1

print(art.contains(b"hello"))  # False
print(art.contains(b"world"))  # True
print(art.contains(b"wor"))  # False
print(pt.contains_prefix(b"hel"))  # False
print(pt.contains_prefix(b"wor"))  # True

print(art.get(b"hello"))  # None
print(art.get(b"world"))  # 2

print(art.insert(b"world", 3))  # 2
print(art.get(b"world"))  # 3

print(art.continuations(b"wo"))  # [(b'world', 3)]

key = b"world cup"
prefix_matches = art.prefix_matches(key)
print(prefix_matches)  # [(5, 3)]
print(
    [(key[:length].decode(), value) for length, value in prefix_matches]
)  # [('world', 3)]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

byte_prefix_tree-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl (238.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

File details

Details for the file byte_prefix_tree-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for byte_prefix_tree-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5be397449133e159f6a9446305b24fd8c6d451ee72b9b3e6c943e2689a3fa609
MD5 321f1a341bf4db69381b92c9b549d5c5
BLAKE2b-256 71f97e9b32f13c4a5d354b515541f3cb4c8d9d97c91d4db613ae3bd26ffe6a3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page