umapR: R6 class representing an unordered map.

umapRR Documentation

R6 class representing an unordered map.

Description

A map is given by keys and values. The keys must be some strings, the values can be any R objects.

Methods

Public methods


Method new()

Creates a new umapR object.

Usage
umapR$new(keys, values, duplicated = "drop", checks = TRUE)
Arguments
keys

the keys, a character vector without NA value

values

the values, a list of R objects; keys and values must have the same length

duplicated

the action to perform for duplicated keys, one of "drop", "join", or "separate"

checks

Boolean, whether to check keys and values

Returns

A umapR object.

Examples
umapR$new(
  keys = c("a", "b"), 
  values = list(c(1, 2), c(3, 4, 5))
)
# examples with duplicated keys:
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6))
)
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6)),
  duplicated = "join"
)
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6)),
  duplicated = "separate"
)

Method print()

Show instance of a umapR object.

Usage
umapR$print(...)
Arguments
...

ignored


Method size()

Size of the map.

Usage
umapR$size()
Returns

An integer, the number of entries.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$size()

Method keys()

Get all keys.

Usage
umapR$keys()
Returns

The keys, a character vector.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$keys()

Method values()

Get all values.

Usage
umapR$values()
Returns

The values, a list of R objects.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$values()

Method items()

Get all entries.

Usage
umapR$items()
Returns

The entries in a dataframe.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$items()

Method toList()

Converts the map to a named list.

Usage
umapR$toList()
Returns

A named list.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$toList()

Method at()

Returns the 'maybe' value corresponding to the given key.

Usage
umapR$at(key)
Arguments
key

a key (string)

Returns

A maybe value, either the value corresponding to the key as a 'Just' maybe value if the key is found, otherwise the 'Nothing' maybe value.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$at("b")
from_just(map$at("b"))
map$at("x")

Method at_unsafe()

Returns the value corresponding to the given key.

Usage
umapR$at_unsafe(key)
Arguments
key

a key (string)

Returns

The value corresponding to the given key if this key is found, otherwise an error.


Method extract()

Extract a submap from the reference map.

Usage
umapR$extract(keys, inplace = FALSE, bydeleting = FALSE)
Arguments
keys

some keys, a character vector; those which do not belong to the keys of the reference map will be ignored

inplace

Boolean, whether to update the reference map or to return a new map

bydeleting

Boolean, whether to construct the submap by deleting the keys which are not in keys or by starting from the empty submap and adding the entries

Returns

Invisibly: a new umapR object if inplace=FALSE, otherwise the updated reference map.

Examples
map <- umapR$new(
  keys = c("a", "b", "c"), 
  values = list(c(1, 2), c(3, 4, 5), c(6, 7))
)
map_copy <- map$copy()
map$extract(c("a", "c"))
map
map$extract(c("a", "c"), inplace = TRUE)
map
map_copy$extract(c("a", "c"), bydeleting = TRUE)
map_copy
map_copy$extract(c("a", "c"), inplace = TRUE, bydeleting = TRUE)
map_copy

Method has_key()

Checks whether a key exists in the reference map.

Usage
umapR$has_key(key)
Arguments
key

a key (string)

Returns

A Boolean value.

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$has_key("b")
map$has_key("x")

Method insert()

Insert a new entry in the reference map.

Usage
umapR$insert(key, value, replace = FALSE)
Arguments
key

a key (string)

value

a value (R object)

replace

Boolean, whether to replace the value if the key is already present

Returns

This updates the reference map and this returns it invisibly. Moreover, a message is printed, describing the update (insertion of a new key, replacement of value, or no change).

Examples
map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$insert("c", c(6, 7)) # insertion
map
map$insert("a", c(8, 9)) # no change
map
map$insert("a", c(8, 9), replace = TRUE) # replacement
map

Method erase()

Erase the entries of the reference map corresponding to the given keys.

Usage
umapR$erase(keys)
Arguments
keys

some keys, a character vector

Returns

The updated reference map, invisibly.

Examples
map <- umapR$new(
  keys = c("a", "b", "c"), 
  values = list(c(1, 2), c(3, 4, 5), c(6, 7))
)
map$erase("a")
map
map$erase(c("b", "c"))
map

Method merge()

Merge the reference map with another map.

Usage
umapR$merge(map, duplicated = "drop")
Arguments
map

a umapR object

duplicated

the action to perform if the reference map and map have some identical keys, one of "drop", "join", or "separate"

Returns

The updated reference map, invisibly.

Examples
map1 <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map2 <- umapR$new(
  keys = c("c", "d"), values = list(c(9, 8), c(7, 6))
)
map1$merge(map2)
map1

# `duplicated` example ####
map1 <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map1_copy1 <- map1$copy()
map1_copy2 <- map1$copy()
map1_copy3 <- map1$copy()
map2 <- umapR$new(
  keys = c("a", "d"), values = list(c(9, 8), c(7, 6))
)
map1_copy1$merge(map2)
map1_copy1

map1_copy2$merge(map2, duplicated = "join")
map1_copy2

map1_copy3$merge(map2, duplicated = "separate")
map1_copy3

Method copy()

Copy the reference map.

Usage
umapR$copy()
Returns

A copy of the reference map.

Examples
map <- umapR$new(c("a", "b"), list(c(1,2), c(FALSE, TRUE)))
true_copy <- map$copy()
true_copy$erase("a")
map
naive_copy <- map
naive_copy$erase("a")
map

See Also

from_just

Examples


## ------------------------------------------------
## Method `umapR$new`
## ------------------------------------------------

umapR$new(
  keys = c("a", "b"), 
  values = list(c(1, 2), c(3, 4, 5))
)
# examples with duplicated keys:
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6))
)
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6)),
  duplicated = "join"
)
umapR$new(
  keys = c("a", "a", "b"), 
  values = list(c(1, 2), c(3, 4), c(5, 6)),
  duplicated = "separate"
)

## ------------------------------------------------
## Method `umapR$size`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$size()

## ------------------------------------------------
## Method `umapR$keys`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$keys()

## ------------------------------------------------
## Method `umapR$values`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$values()

## ------------------------------------------------
## Method `umapR$items`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$items()

## ------------------------------------------------
## Method `umapR$toList`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$toList()

## ------------------------------------------------
## Method `umapR$at`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$at("b")
from_just(map$at("b"))
map$at("x")

## ------------------------------------------------
## Method `umapR$extract`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b", "c"), 
  values = list(c(1, 2), c(3, 4, 5), c(6, 7))
)
map_copy <- map$copy()
map$extract(c("a", "c"))
map
map$extract(c("a", "c"), inplace = TRUE)
map
map_copy$extract(c("a", "c"), bydeleting = TRUE)
map_copy
map_copy$extract(c("a", "c"), inplace = TRUE, bydeleting = TRUE)
map_copy

## ------------------------------------------------
## Method `umapR$has_key`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$has_key("b")
map$has_key("x")

## ------------------------------------------------
## Method `umapR$insert`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map$insert("c", c(6, 7)) # insertion
map
map$insert("a", c(8, 9)) # no change
map
map$insert("a", c(8, 9), replace = TRUE) # replacement
map

## ------------------------------------------------
## Method `umapR$erase`
## ------------------------------------------------

map <- umapR$new(
  keys = c("a", "b", "c"), 
  values = list(c(1, 2), c(3, 4, 5), c(6, 7))
)
map$erase("a")
map
map$erase(c("b", "c"))
map

## ------------------------------------------------
## Method `umapR$merge`
## ------------------------------------------------

map1 <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map2 <- umapR$new(
  keys = c("c", "d"), values = list(c(9, 8), c(7, 6))
)
map1$merge(map2)
map1

# `duplicated` example ####
map1 <- umapR$new(
  keys = c("a", "b"), values = list(c(1, 2), c(3, 4, 5))
)
map1_copy1 <- map1$copy()
map1_copy2 <- map1$copy()
map1_copy3 <- map1$copy()
map2 <- umapR$new(
  keys = c("a", "d"), values = list(c(9, 8), c(7, 6))
)
map1_copy1$merge(map2)
map1_copy1

map1_copy2$merge(map2, duplicated = "join")
map1_copy2

map1_copy3$merge(map2, duplicated = "separate")
map1_copy3

## ------------------------------------------------
## Method `umapR$copy`
## ------------------------------------------------

map <- umapR$new(c("a", "b"), list(c(1,2), c(FALSE, TRUE)))
true_copy <- map$copy()
true_copy$erase("a")
map
naive_copy <- map
naive_copy$erase("a")
map

stla/mapR documentation built on April 16, 2022, 11:54 p.m.