Skip to content

Commit

Permalink
Add the Room class (#26)
Browse files Browse the repository at this point in the history
* Add new internal functions: all_pairs and all_ordered_pairs.

* Add the Room R6 class.

* Bump development version number.
  • Loading branch information
MHenderson authored Jul 23, 2024
1 parent 1939e83 commit d9c85f0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: wallis
Title: Room squares in R
Version: 0.0.0.9006
Version: 0.0.0.9007
Authors@R:
person("Matthew", "Henderson", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-7949-8208"))
Expand All @@ -12,5 +12,6 @@ RoxygenNote: 7.3.1
Imports:
dplyr,
purrr,
R6,
tibble,
tidyr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(Room)
export(empty_cells)
export(empty_room)
export(is_maximal_proom)
Expand All @@ -8,3 +9,4 @@ export(is_room)
export(n_filled_cells)
export(unused_pairs)
export(volume)
importFrom(R6,R6Class)
32 changes: 32 additions & 0 deletions R/all-pairs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' All unordered pairs
#'
#' @param n Size of underlying set.
#'
#' @return A list of all unordered pairs.
all_pairs <- function(n) {

y <- combn(0:(n - 1), 2)

tibble::tibble(
first = y[1,],
second = y[2,]
) |>
dplyr::mutate(ffs = purrr::map2(first, second, c)) |>
dplyr::pull(ffs)

}

#' All ordered pairs
#'
#' @param n Size of underlying set
#'
#' @return A list of all ordered pairs.
all_ordered_pairs <- function(n) {

y <- tidyr::expand_grid(i = 1:n, j = 1:n)

y |>
dplyr::mutate(ffs = purrr::map2(i, j, c)) |>
dplyr::pull(ffs)

}
65 changes: 65 additions & 0 deletions R/room-class.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Create a Room square
#'
#' @docType class
#' @importFrom R6 R6Class
#'
#' @param size the order of the Room square to be created
#'
#' @export
#' @format An \code{\link{R6Class}} generator object
Room <- R6::R6Class(
classname = "Room",
public = list(

size = NULL,
cells = NULL,
symbols = NULL,
free_pairs = NULL,
empty_cells = NULL,

initialize = function(size = NA) {

self$size <- size
self$symbols <- 0:(self$size - 1)

self$cells <- tidyr::expand_grid(row = 1:(self$size - 1), col = 1:(self$size - 1)) |>
dplyr::mutate(first = as.integer(NA), second = as.integer(NA)) |>
dplyr::mutate(avail = list(0:(self$size - 1)))

self$free_pairs <- all_pairs(self$size)
self$empty_cells <- all_ordered_pairs(self$size - 1)

},

set = function(e, p) {

self$cells[self$cells$row == e[1] & self$cells$col == e[2], "first"] <- p[1]
self$cells[self$cells$row == e[1] & self$cells$col == e[2], "second"] <- p[2]

self$cells[self$cells$row == e[1], "avail"]$avail <- lapply(self$cells[self$cells$row == e[1], "avail"]$avail, remove_both, p)
self$cells[self$cells$col == e[2], "avail"]$avail <- lapply(self$cells[self$cells$col == e[2], "avail"]$avail, remove_both, p)

self$free_pairs <- self$free_pairs[-match(list(p), self$free_pairs)]
self$empty_cells <- self$empty_cells[-match(list(e), self$empty_cells)]

},

is_available = function(e, p) {
p[1] %in% self$cells[self$cells$row == e[1] & self$cells$col == e[2], "avail"]$avail[[1]] && p[2] %in% self$cells[self$cells$row == e[1] & self$cells$col == e[2], "avail"]$avail[[1]]
}

),
active = list(

n_filled = function() {
self$cells |>
dplyr::filter(!is.na(first)) |>
nrow()
},

volume = function() {
round(self$n_filled/choose(max(self$cells$col) + 1, 2), 6)
}

)
)
75 changes: 75 additions & 0 deletions man/Room.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/all_ordered_pairs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/all_pairs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9c85f0

Please sign in to comment.