File: nextPerm.R

package info (click to toggle)
r-cran-multicool 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 160 kB
  • sloc: cpp: 420; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 882 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
#' Return the next permutation of the multiset
#' 
#' This function returns the next permuation of the multiset if there is one.
#' \code{initMC} called before \code{nextPerm} can be called.
#' 
#' 
#' @param mcObj an S3 object of class \code{mc} which must be created with
#' \code{initMC}
#' @return either a vector with the next permutation of the multiset or
#' \code{FALSE} when all permutations have been returned
#' @author James M. Curran
#' @seealso nextPerm
#' @examples
#' 
#' x = c(1,1,2,2)
#' m1 = initMC(x)
#' 
#' for(i in 1:6){
#'   cat(paste(paste(nextPerm(m1),collapse=","),"\n"))
#' }
#' 
#' ## an example with letters
#' x = letters[1:4]
#' m2 = initMC(x)
#' nextPerm(m2)
#' nextPerm(m2)
#' ## and so on
#' 
#' @export nextPerm
nextPerm = function(mcObj){
  r = mcObj$mc$nextPerm()
  
  if(r$b){
    return(mcObj$elements[r$set])
  }else{
    return(FALSE)
  }
}