xhash: Create Hash Table for Generic States

Description Usage Arguments Value See Also Examples

View source: R/xhash.R

Description

This function creates an xhash object, extended version of hash. While hash accepts only strings as indices, xhash can deal with generic index variables, termed as "state".

Usage

1
2
xhash(convfunc = function(state, ...) state, convfunc_vec = function(states,
  ...) unlist(Map(convfunc, states, ...)), default_value = NULL)

Arguments

convfunc

function that converts a state to a key. It must take a positional argument state and keyword arguments represented by ..., and returns a character.

convfunc_vec

function for vectorized conversion from states to keys. This function must receive a positional argument states and keyword arguments ... and returns character vector. By default, it vectorizes convfunc using Map. User may specify a more efficient function if any.

default_value

value to be returned when a state is not recorded in the table.

Value

xhash object

See Also

hash-ops, vectorized-hash-ops

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
h <- xhash(convfunc = function(state, ...) paste0(state, collapse='-'))

# insert
h[c(1, 2, 3)] <- 100
h[matrix(1:9, nrow=3, ncol=3)] <- -5

# retrieve
h[c(1, 2, 3)]
h[matrix(1:9, nrow=3, ncol=3)]
h[1:9]          # equivalent as above, due to conversion to a same key
h[c(3, 2, 1)]   # this is undefined

# delete
h[c(1, 2, 3)] <- NULL

# vectorized operations
## insert
setvalues(h, list(1:2, 1:3), c(9, 8))
## retrieve
getvalues(h, list(1:9, 1:2, 3:1))
## delete
setvalues(h, list(1:9, 1:3), NULL)

kota7/tictactoe documentation built on May 20, 2019, 1:11 p.m.