Title: | Combinatorics Iterators |
---|---|
Description: | Provides iterators for combinations, permutations, subsets, and Cartesian product, which allow one to go through all elements without creating a huge set of all possible values. |
Authors: | Kota Mori [aut, cre] |
Maintainer: | Kota Mori <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.3 |
Built: | 2024-11-03 04:51:02 UTC |
Source: | https://github.com/kota7/combiter |
getFirst
is a generic function that returns the
first value of iterators
getFirst(obj, ...)
getFirst(obj, ...)
obj |
an R object |
... |
additional arguments |
iterator value, format dependes on the objects
getFirst
is a generic function that returns the
last value of iterators
getLast(obj, ...)
getLast(obj, ...)
obj |
an R object |
... |
additional arguments |
iterator value, format dependes on the objects
hasPrev
is a generic function that indicates if the
iterator has another element backward.
hasPrev(obj, ...)
hasPrev(obj, ...)
obj |
an R object |
... |
additional arguments |
Logical value indicating whether the iterator has a previous element.
Create an iterator going through Cartesian product of several items.
icartes(nvec) icartesv(...)
icartes(nvec) icartesv(...)
nvec |
integer vector of number of items |
... |
set of iterables (subsettable by |
icartes
iterates through all combinations of integers
icartesv
iterates through all combinations of general values
iterator object
x <- icartes(c(3, 2, 4)) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } x <- icartesv(Month=c("Jan", "Feb", "Mar"), Loc=c("NY", "LA"), By=c("car", "plane", "bus")) as.list(x)
x <- icartes(c(3, 2, 4)) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } x <- icartesv(Month=c("Jan", "Feb", "Mar"), Loc=c("NY", "LA"), By=c("car", "plane", "bus")) as.list(x)
Create an iterator for all combinations k integers out of 1 through n.
icomb(n, k) icombv(values, k)
icomb(n, k) icombv(values, k)
n |
positive integer |
k |
positive integer no greater than n |
values |
iterable (subsettable by |
icomb
iterates through integer vectors
icombv
iterates through general values
iterator object
x <- icomb(5, 3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(icombv(c("A", "G", "C"), 2))
x <- icomb(5, 3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(icombv(c("A", "G", "C"), 2))
Create an iterator for all permutations of size k of integers 1 to n.
iperm(n, k = n) ipermv(values, k = length(values))
iperm(n, k = n) ipermv(values, k = length(values))
n |
positive integer |
k |
positive integer |
values |
iterable (subsettable by |
iperm
iterates through integer vectors
ipermv
iterates through general values
iterator object
x <- iperm(3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(ipermv(c("R", "G", "B")))
x <- iperm(3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(ipermv(c("R", "G", "B")))
Create an iterator for all subsets of integers 1 through n.
isubset(n) isubsetv(values)
isubset(n) isubsetv(values)
n |
positive integer |
values |
iterable (subsettable by |
isubset
iterates through integer vectors
isubsetv
iterates through general values
iterator object
x <- isubset(3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(isubsetv(letters[1:4]))
x <- isubset(3) ct <- 0 while (hasNext(x)) { ct <- ct + 1 i <- nextElem(x) cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " "))) } as.list(isubsetv(letters[1:4]))
prevElem
is a generic funcion to move an
iterator object one step backward.
prevElem(obj, ...)
prevElem(obj, ...)
obj |
an R object |
... |
additional arguments |
iterator value
This is a constructor for custom iterator objects. It requires four functions, "next", "prev", "first", and "last", and additional parameters.
The state of the constructor is characterized by the variable i
.
The "next" and "prev" function must take i
and the parameters
and return the next and previous state variables respectively. The behavior where there is no more state left is arbitrary.
The "first" and "last" functions must take the additional parameters and return the initial and last state variables respectively.
The created object is an iterator of class recursiveiter
, which inherits
abstractiter
and iter
.
It can be used with foreach
and accepts as.list
conversion.
recursiveiter(nextFunc, prevFunc, firstFunc, lastFunc, ...)
recursiveiter(nextFunc, prevFunc, firstFunc, lastFunc, ...)
nextFunc , prevFunc
|
Functions that take the iterator state and the parameters |
firstFunc , lastFunc
|
Functions that take the parameters |
... |
additional parameters of the iterator |
iterator object
fibiter <- recursiveiter( nextFunc = function(i) if (length(i)==1 && i==0) 1 else if (length(i)==1 && i==1) c(1,1) else c(sum(i), i[1]), prevFunc = NULL, firstFunc = function() 0, lastFunc = function() Inf) for (k in 1:20) cat(nextElem(fibiter)[1], "")
fibiter <- recursiveiter( nextFunc = function(i) if (length(i)==1 && i==0) 1 else if (length(i)==1 && i==1) c(1,1) else c(sum(i), i[1]), prevFunc = NULL, firstFunc = function() 0, lastFunc = function() Inf) for (k in 1:20) cat(nextElem(fibiter)[1], "")