Repository navigation
Expand file tree
/
Copy pathposition.R
More file actions
163 lines (140 loc) · 4.1 KB
/
Copy pathposition.R
File metadata and controls
163 lines (140 loc) · 4.1 KB
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
#' @title
#' Get index positions from a Hilbert Curve
#' @param h
#' One of: Integer vector, `data.frame`, or `matrix`.
#' @param ... Unused.
#' @param n
#' Exponent to the dimensions of the underlying grid. The Hilbert
#' Curve indices are based on a `2^n x 2^n` grid. This number
#' must be less than 15 due to the 32-bit implementation of R. This *must*
#' be the same as the `n` used in `index`.
#' @param idx
#' Column name or index containing the Hilbert Curve indices.
#' @param attach
#' If `TRUE`, adds the position as new columns to the given
#' `data.frame`/`matrix`. If `h` is a `data.frame`, then the
#' columns are named `x` and `y`; otherwise, it is two unnamed columns at
#' the end of the matrix.
#' @return A `data.frame` containing the positions as `integer`
#' columns `x` and `y`, or the original object
#' (`data.frame` or `matrix`) with the columns attached.
#' When `n` is greater than 15, the positions are of
#' type `bit64::integer64`.
#' @rdname position
#' @export
position <- function(h, ..., n = 10L) {
if (n < 16L) {
UseMethod("position")
} else {
if (!requireNamespace("bit64", quietly = TRUE)) {
stop("`bit64` is required to use exponents greater than 15.")
}
UseMethod("position64")
}
}
#' @rdname position
#' @export
position.data.frame <- function(h, ..., n, idx = 1, attach = TRUE) {
.Class <- class(h[[idx[1]]])
positions <- NextMethod("position", h = h[[idx[1]]], ..., n = n)
if (attach) {
h[["x"]] <- positions[[1]]
h[["y"]] <- positions[[2]]
return(h)
}
positions
}
#' @rdname position
#' @export
position.matrix <- function(h, ..., n, idx = 1, attach = TRUE) {
.Class <- class(h[, idx[1]])
positions <- NextMethod("position", h = h[, idx[1]], ..., n = n)
if (attach) {
h[, ncol(h) + 1] <- positions[[1]]
h[, ncol(h) + 1] <- positions[[2]]
return(h)
}
positions
}
#' @rdname position
#' @export
position.numeric <- function(h, ..., n) {
.Class <- "integer"
NextMethod("position", h = as.integer(h), ..., n = n)
}
#' @rdname position
#' @export
position.integer <- function(h, ..., n) {
HILBERT_position_(n, h)
}
#' @rdname position
#' @export
position64 <- function(h, ..., n = 10L) {
UseMethod("position64")
}
#' @rdname position
#' @export
position64.data.frame <- function(h, ..., n, idx = 1, attach = TRUE) {
.Class <- class(h[[idx[1]]])
positions <- NextMethod("position64", h = h[[idx[1]]], ..., n = n)
if (attach) {
h[["x"]] <- positions[[1]]
h[["y"]] <- positions[[2]]
return(h)
}
positions
}
#' @rdname position
#' @export
position64.matrix <- function(h, ..., n, idx = 1, attach = TRUE) {
.Class <- class(h[, idx[1]])
positions <- NextMethod("position64", h = h[[idx[1]]], ..., n = n)
if (attach) {
h[, ncol(h) + 1] <- positions[[1]]
h[, ncol(h) + 1] <- positions[[2]]
return(h)
}
positions
}
#' @rdname position
#' @export
position64.double <- function(h, ..., n) {
h <- bit64::as.integer64(h)
.Class <- "integer64"
NextMethod("position64", h = h, ..., n = n)
}
#' @rdname position
#' @export
position64.integer <- function(h, ..., n) {
h <- bit64::as.integer64(h)
.Class <- "integer64"
NextMethod("position64", h = h, ..., n = n)
}
#' @rdname position
#' @export
position64.numeric <- function(h, ..., n) {
h <- bit64::as.integer64(h)
.Class <- "integer64"
NextMethod("position64", h = h, ..., n = n)
}
#' @rdname position
#' @export
position64.integer64 <- function(h, ..., n) {
h <- bit64::as.bitstring(h)
.Class <- "bitstring"
NextMethod("position64", h = h, ..., n = n)
}
#' @rdname position
#' @export
position64.character <- function(h, ..., n) {
.Class <- class(h) <- "bitstring"
NextMethod("position64", h = h, ..., n = n)
}
#' @rdname position
#' @export
position64.bitstring <- function(h, ..., n) {
pos <- HILBERT_position64_(n, h)
pos[[1]] <- bit64::as.integer64(pos[[1]])
pos[[2]] <- bit64::as.integer64(pos[[2]])
pos
}