Collections for R are collections as can be found in Python's collections module implemented using Rcpp.
Very much a work in progress.
At the moment recollections exposes the following C++ STL data structures to R:
- Bounded priority queue based on
std::map
- Dictionary (hash table) based on
std::unordered_map
- Priority queue based on
std::priority_queue
- String Builder (custom implementation based on
std::vector<std::string>
and several sequences:
- Cyclic Sequence
- Prime Sequence
- Step Sequence
which can be accessed using nextItem(sequence)
, take(sequence, n)
and
takeWhile(sequence, fun)
.
# Function and indexing style
dict <- recollections::dictionary()
dict['foo'] <- 1L
setValue(dict, 'bar', 2L)
dict['foo'] # 1L
getValue(dict, 'bar') # 2L
# Pipes
dict <- recollections::dictionary() |>
setValue('foo', 1L) |>
setValue('bar', 2L)
dict['bar'] # 2L
dict['foo'] # 1L
cycleSeq <- cycleSequence(5, 25, 5)
unlist(take(cycleSeq, 10L)) # [1] 5 10 15 20 25 10 15 20 25 10
primeSeq <- recollections::primeSequence()
for (i in 1:10) {
cat(nextItem(primeSeq), ' ') # 2 3 5 7 11 13 17 19 23 29
}
unlist(takeWhile(primeSeq, \(val) val < 50L)) # [1] 31 37 41 43 47
unlist(take(primeSeq, 5L)) # [1] 53 59 61 67 71
stepSeq <- stepSequence(5L, 4L)
unlist(take(stepSeq, 10L)) # [1] 5 9 13 17 21 25 29 33 37 41
The data structures have the following S3-methods (where applicable):
clear()
clears the contents of a containerempty()
check for emptinessfilter()
find all elements that satisfy a testkeys()
gathers all keys in a containermap()
applies a function to all values in a containerpop()
removes the top item from a containerpush()
adds an element to a containersetValue()
sets a value for a keysize()
the size of a containertop()
the top item from a containertopAndPop()
returns and removes the top element of a container