Title: | 'Radix' Trees in 'Rcpp' |
---|---|
Description: | 'Radix trees', or 'tries', are key-value data structures optimised for efficient lookups, similar in purpose to hash tables. 'triebeard' provides an implementation of 'radix trees' for use in R programming and in developing packages with 'Rcpp'. |
Authors: | Os Keyes [aut, cre], Drew Schmidt [aut], Yuuki Takano [cph] |
Maintainer: | Os Keyes <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.1 |
Built: | 2025-03-06 04:33:41 UTC |
Source: | https://github.com/ironholds/triebeard |
trie_add
and trie_remove
allow you to
add or remove entries from tries, respectively.
trie_add(trie, keys, values) trie_remove(trie, keys)
trie_add(trie, keys, values) trie_remove(trie, keys)
trie |
a trie object created with |
keys |
a character vector containing the keys of the entries to add (or remove). Entries with NA keys will not be added. |
values |
an atomic vector, matching the type of the trie, containing the values of the entries to add. Entries with NA values will not be added. |
nothing; the trie is modified in-place
trie
for creating tries in the first place.
trie <- trie("foo", "bar") length(trie) trie_add(trie, "baz", "qux") length(trie) trie_remove(trie, "baz") length(trie)
trie <- trie("foo", "bar") length(trie) trie_add(trie, "baz", "qux") length(trie) trie_remove(trie, "baz") length(trie)
"Getters" for the data stored in a trie object. get_keys
gets the keys, get_values
gets the values.
get_keys(trie) get_values(trie)
get_keys(trie) get_values(trie)
trie |
A trie object, created with |
An atomic vector of keys or values stored in the trie.
greedy_match
accepts a trie and a character vector
and returns the values associated with any key that is "greedily" (read: fuzzily)
matched against one of the character vector entries.
greedy_match(trie, to_match, include_keys = FALSE)
greedy_match(trie, to_match, include_keys = FALSE)
trie |
a trie object, created with |
to_match |
a character vector containing the strings to check against the trie's keys. |
include_keys |
a logical value indicating whether to include the keys in the returned results or not. If TRUE (not the default) the returned object will be a list of data.frames, rather than of vectors. |
a list, the length of to_match
, with each entry containing any trie values
where the to_match
element greedily matches the associated key. In the case that
nothing was found, the entry will contain NA
. In the case that include_keys
is TRUE, the matching keys will also be included
longest_match
and prefix_match
for longest and prefix matching, respectively.
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) greedy_match(trie, c("avoid", "bring", "attack"))
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) greedy_match(trie, c("avoid", "bring", "attack"))
longest_match
accepts a trie and a character vector
and returns the value associated with whichever key had the longest match
to each entry in the character vector. A trie of "binary" and "bind", for example,
with an entry-to-compare of "binder", will match to "bind".
longest_match(trie, to_match, include_keys = FALSE)
longest_match(trie, to_match, include_keys = FALSE)
trie |
a trie object, created with |
to_match |
a character vector containing the strings to match against the trie's keys. |
include_keys |
a logical value indicating whether to include the keys in the returned results or not. If TRUE (not the default) the returned object will be a data.frame, rather than a vector. |
prefix_match
and greedy_match
for prefix and greedy matching, respectively.
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) longest_match(trie, "binder")
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) longest_match(trie, "binder")
prefix_match
accepts a trie and a character vector
and returns the values associated with any key that has a particular
character vector entry as a prefix (see the examples).
prefix_match(trie, to_match, include_keys = FALSE)
prefix_match(trie, to_match, include_keys = FALSE)
trie |
a trie object, created with |
to_match |
a character vector containing the strings to check against the trie's keys. |
include_keys |
a logical value indicating whether to include the keys in the returned results or not. If TRUE (not the default) the returned object will be a list of data.frames, rather than of vector. |
a list, the length of to_match
, with each entry containing any trie values
where the to_match
element was a prefix of the associated key. In the case that
nothing was found, the entry will contain NA
.
longest_match
and greedy_match
for longest and greedy matching, respectively.
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) prefix_match(trie, "aff")
trie <- trie(keys = c("afford", "affair", "available", "binary", "bind", "blind"), values = c("afford", "affair", "available", "binary", "bind", "blind")) prefix_match(trie, "aff")
create_trie
creates a trie (a key-value store optimised
for matching) out of a provided character vector of keys, and a numeric,
character, logical or integer vector of values (both the same length).
trie(keys, values)
trie(keys, values)
keys |
a character vector containing the keys for the trie. |
values |
an atomic vector of any type, containing the values to pair with
|
a 'trie' object.
trie_add
and trie_remove
for adding to and removing
from tries after their creation, and longest_match
and other match functions
for matching values against the keys of a created trie.
# An integer trie int_trie <- trie(keys = "foo", values = 1) # A string trie str_trie <- trie(keys = "foo", values = "bar")
# An integer trie int_trie <- trie(keys = "foo", values = 1) # A string trie str_trie <- trie(keys = "foo", values = "bar")
This package provides access to Radix tree (or "trie") structures in Rcpp. At a later date it will hopefully provide them in R, too.