| 1 |
|
| 2 |
renv_version_compare <- function(lhs, rhs, n = NULL) { |
| 3 |
|
| 4 |
# retrieve versions as integer vector |
| 5 |
lhs <- unlist(unclass(numeric_version(lhs))) |
| 6 |
rhs <- unlist(unclass(numeric_version(rhs))) |
| 7 |
|
| 8 |
# compute number of components to compare |
| 9 |
n <- n %||% max(length(lhs), length(rhs)) |
| 10 |
|
| 11 |
# pad each vector with zeroes up to the requested length |
| 12 |
lhs <- c(lhs, rep.int(0L, max(0L, n - length(lhs)))) |
| 13 |
rhs <- c(rhs, rep.int(0L, max(0L, n - length(rhs)))) |
| 14 |
|
| 15 |
# iterate through each component and compare |
| 16 |
for (i in seq_len(n)) { |
| 17 |
if (lhs[[i]] < rhs[[i]]) |
| 18 |
return(-1L) |
| 19 |
else if (lhs[[i]] > rhs[[i]]) |
| 20 |
return(+1L) |
| 21 |
} |
| 22 |
|
| 23 |
# if we got here, then all components compared equal |
| 24 |
0L |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
renv_version_le <- function(lhs, rhs, n = NULL) { |
| 29 |
renv_version_compare(lhs, rhs, n) <= 0L |
| 30 |
} |
| 31 |
|
| 32 |
renv_version_lt <- function(lhs, rhs, n = NULL) { |
| 33 |
renv_version_compare(lhs, rhs, n) < 0L |
| 34 |
} |
| 35 |
|
| 36 |
renv_version_eq <- function(lhs, rhs, n = NULL) { |
| 37 |
renv_version_compare(lhs, rhs, n) == 0L |
| 38 |
} |
| 39 |
|
| 40 |
renv_version_gt <- function(lhs, rhs, n = NULL) { |
| 41 |
renv_version_compare(lhs, rhs, n) > 0L |
| 42 |
} |
| 43 |
|
| 44 |
renv_version_ge <- function(lhs, rhs, n = NULL) { |
| 45 |
renv_version_compare(lhs, rhs, n) >= 0L |
| 46 |
} |
| 47 |
|
| 48 |
renv_version_match <- function(versions, request) { |
| 49 |
|
| 50 |
nrequest <- unclass(numeric_version(request))[[1L]] |
| 51 |
for (i in rev(seq_along(nrequest))) { |
| 52 |
|
| 53 |
matches <- which(map_lgl(versions, function(version) { |
| 54 |
renv_version_eq(version, request, n = i) |
| 55 |
})) |
| 56 |
|
| 57 |
if (!length(matches)) |
| 58 |
next |
| 59 |
|
| 60 |
# TODO: should '3.1' match the closest match (e.g. '3.2') or |
| 61 |
# highest match (e.g. '3.6')? |
| 62 |
sorted <- matches[sort(names(matches), decreasing = TRUE)] |
| 63 |
return(names(sorted)[[1L]]) |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
versions[[1L]] |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
renv_version_parts <- function(version, n) { |
| 72 |
|
| 73 |
# split version into parts |
| 74 |
parts <- unclass(as.numeric_version(version))[[1L]] |
| 75 |
|
| 76 |
# extend parts to size of n |
| 77 |
diff <- max(n) - length(parts) |
| 78 |
if (diff > 0) |
| 79 |
parts <- c(parts, rep.int(0L, diff)) |
| 80 |
|
| 81 |
# retrieve possibly-extended parts |
| 82 |
parts[1:n] |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
renv_version_maj_min <- function(version) { |
| 87 |
parts <- renv_version_parts(version, 2L) |
| 88 |
paste(parts, collapse = ".") |
| 89 |
} |
| 90 |
|
| 91 |
renv_version_length <- function(version) { |
| 92 |
nv <- as.numeric_version(version) |
| 93 |
length(unclass(nv)[[1L]]) |
| 94 |
} |
| 95 |
|
| 96 |
# parse version strings into an integer matrix with one row per |
| 97 |
# version. the number of columns is determined by the version with |
| 98 |
# the most components; shorter versions are zero-padded on the right. |
| 99 |
# |
| 100 |
# NOTE: versions must be non-NA character strings parseable by |
| 101 |
# numeric_version(); NA or non-numeric components are not supported. |
| 102 |
# |
| 103 |
# replaced by C implementation when ext is enabled. |
| 104 |
renv_version_matrix <- function(versions) { |
| 105 |
|
| 106 |
parts <- strsplit(versions, "[.-]", perl = TRUE) |
| 107 |
lens <- lengths(parts, use.names = FALSE) |
| 108 |
maxlen <- max(lens) |
| 109 |
|
| 110 |
flat <- unlist(parts, use.names = FALSE) |
| 111 |
row <- rep.int(seq_along(versions), lens) |
| 112 |
col <- sequence(lens) |
| 113 |
|
| 114 |
n <- length(versions) |
| 115 |
mat <- matrix(0L, nrow = n, ncol = maxlen) |
| 116 |
mat[row + (col - 1L) * n] <- as.integer(flat) |
| 117 |
|
| 118 |
mat |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
# produce an integer ranking of version strings, suitable for use |
| 123 |
# with order(). avoids the overhead of numeric_version() by splitting |
| 124 |
# version strings into an integer matrix and ranking the result. |
| 125 |
renv_version_rank <- function(versions) { |
| 126 |
|
| 127 |
mat <- renv_version_matrix(versions) |
| 128 |
|
| 129 |
# order then rank, so the result can be composed with other |
| 130 |
# sort keys via order(packages, renv_version_rank(versions)) |
| 131 |
cols <- lapply(seq_len(ncol(mat)), function(i) mat[, i]) |
| 132 |
ord <- do.call(order, cols) |
| 133 |
rank <- integer(length(versions)) |
| 134 |
rank[ord] <- seq_along(ord) |
| 135 |
rank |
| 136 |
|
| 137 |
} |