File: create.R

package info (click to toggle)
r-cran-triebeard 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: cpp: 1,095; sh: 13; makefile: 2; ansic: 1
file content (52 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#'@title Create a Trie
#'@description \code{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).
#'
#'@param keys a character vector containing the keys for the trie.
#'
#'@param values an atomic vector of any type, containing the values to pair with
#'\code{keys}. Must be the same length as \code{keys}.
#'
#'@return a `trie` object.
#'
#'@seealso \code{\link{trie_add}} and \code{\link{trie_remove}} for adding to and removing
#'from tries after their creation, and \code{\link{longest_match}} and other match functions
#'for matching values against the keys of a created trie.
#'
#'@examples
#'# An integer trie
#'int_trie <- trie(keys = "foo", values = 1)
#'
#'# A string trie
#'str_trie <- trie(keys = "foo", values = "bar")
#'
#'@export
trie <- function(keys, values){
  stopifnot(length(keys) == length(values))
  stopifnot(is.character(keys))
  output <- NULL
  output_classes <- c("trie", NA)

  switch(class(values)[1],
         "character" = {
           output <- radix_create_string(keys, values)
           output_classes[2] <- "string_trie"
         },
         "integer" = {
           output <- radix_create_integer(keys, values)
           output_classes[2] <- "integer_trie"
         },
         "numeric" = {
           output <- radix_create_numeric(keys, values)
           output_classes[2] <- "numeric_trie"
         },
         "logical" = {
           output <- radix_create_logical(keys, values)
           output_classes[2] <- "logical_trie"
         },
         stop("'values' must be a numeric, integer, character or logical vector"))

  class(output) <- c(class(output), output_classes)
  return(output)
}