-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpartitions.R
174 lines (168 loc) · 5.54 KB
/
partitions.R
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#' Number of partitions
#' @param n an non-negative integer to be partitioned
#' @param k number of parts
#' @param distinct an logical to restrict distinct values
#' @param bigz an logical to use [gmp::bigz]
#' @seealso [partitions] for generating all partitions and [ipartitions] for iterating partitions
#' @examples
#' # number of partitions of 10
#' npartitions(10)
#' # number of partitions of 10 into 5 parts
#' npartitions(10, 5)
#'
#' # integer overflow
#' \dontrun{npartitions(160)}
#' npartitions(160, bigz = TRUE)
#'
#' # zero sized partitions
#' npartitions(0)
#' npartitions(5, 0)
#' npartitions(5, 6)
#' npartitions(0, 0)
#' npartitions(0, 1)
#' @export
npartitions <- function(n, k = NULL, distinct = FALSE, bigz = FALSE) {
.Call(C_npartitions, n, k, distinct, bigz)
}
#' Partitions generator
#'
#' This function partitions an non-negative interger `n` into `k` parts or parts of any sizes.
#' The results are in lexicographical or reversed lexicographical order.
#'
#' @param n an non-negative integer to be partitioned
#' @param k number of parts
#' @param distinct an logical to restrict distinct values
#' @param descending an logical to use reversed lexicographical order
#' @template param_type
#' @param nitem number of partitions required, usually used with \code{skip}
#' @param skip the number of partitions skipped
#' @param index a vector of indices of the desired partitions
#' @param nsample sampling random partitions
#' @param drop vectorize a matrix or unlist a list
#' @seealso [ipartitions] for iterating partitions and [npartitions] to calculate number of partitions
#' @examples
#' # all partitions of 6
#' partitions(6)
#' # reversed lexicographical order
#' partitions(6, descending = TRUE)
#'
#' # fixed number of parts
#' partitions(10, 5)
#' # reversed lexicographical order
#' partitions(10, 5, descending = TRUE)
#'
#' # column major
#' partitions(6, layout = "column")
#' partitions(6, 3, layout = "column")
#'
#' # list output
#' partitions(6, layout = "list")
#' partitions(6, 3, layout = "list")
#'
#' # zero sized partitions
#' dim(partitions(0))
#' dim(partitions(5, 0))
#' dim(partitions(5, 6))
#' dim(partitions(0, 0))
#' dim(partitions(0, 1))
#'
#' @export
partitions <- function(n, k = NULL, distinct = FALSE, descending = FALSE, layout = NULL,
nitem = -1L, skip = NULL, index = NULL, nsample = NULL, drop = NULL) {
.Call(C_get_partitions,
n, k, distinct, descending, layout, nitem, index, nsample, NULL, skip, drop)
}
#' @details
#' The `Partitions` class can be initialized by using the convenient wrapper `ipartitions` or
#' \preformatted{
#' Partitions$new(n, k = NULL, descending = FALSE)
#' }
#' @template iterator_methods
#' @rdname ipartitions
#' @export
Partitions <- R6::R6Class(
"Partitions",
inherit = Arrangements,
private = list(
state = NULL,
null_pending = FALSE
),
public = list(
n = NULL,
k = NULL,
distinct = NULL,
descending = NULL,
skip = NULL,
initialize = function(n, k = NULL, distinct = FALSE, descending = FALSE, skip = NULL) {
self$n <- as.integer(n)
if (!is.null(k)) {
self$k <- as.integer(k)
}
self$distinct <- distinct
self$descending <- descending
self$skip <- skip
self$reset()
},
reset = function() {
private$state <- new.env()
private$state$null_pending <- FALSE
},
collect = function(layout = "row") {
out <- self$getnext(-1L, layout, drop = FALSE)
self$reset()
out
},
getnext = function(d = 1L, layout = NULL, drop = NULL) {
if (d > 0 && private$state$null_pending) {
out <- NULL
self$reset()
} else {
out <- .Call(C_get_partitions,
self$n, self$k, self$distinct, self$descending, layout, d, NULL, NULL,
private$state, self$skip, drop)
is.null(out) && self$reset()
}
out
},
print = function(...) {
if (is.null(self$k)) {
cat("Partitions of", self$n, "\n")
} else {
cat("Partitions of", self$n, "into", self$k, "parts\n")
}
invisible(self)
}
)
)
#' @title Partitions iterator
#' @description
#' This function returns a [Partitions] iterator for iterating
#' partitions of an non-negative integer `n` into `k` parts or parts of any sizes.
#' The iterator allows users to fetch the next partition(s) via the `getnext()` method.
#'
#' @param n an non-negative integer to be partitioned
#' @param k number of parts
#' @param distinct an logical to restrict distinct values
#' @param descending an logical to use reversed lexicographical order
#' @param skip the number of partitions skipped
#' @seealso [partitions] for generating all partitions and [npartitions] to calculate number of partitions
#' @examples
#' ipart <- ipartitions(10)
#' ipart$getnext()
#' ipart$getnext(2)
#' ipart$getnext(layout = "column", drop = FALSE)
#' # collect remaining partitions
#' ipart$collect()
#'
#' library(foreach)
#' foreach(x = ipartitions(6, 2), .combine=c) %do% {
#' prod(x)
#' }
#' @export
ipartitions <- function(n, k = NULL, distinct = FALSE, descending = FALSE, skip = NULL) {
(n %% 1 == 0 && n >= 0) || stop("expect integer")
if (!is.null(k)) {
(k %% 1 == 0 && k >= 0) || stop("expect integer")
}
Partitions$new(n, k, distinct, descending, skip)
}