Skip to content

Commit ccaf32a

Browse files
committed
Polishing terminology and code
1 parent 094183a commit ccaf32a

9 files changed

Lines changed: 372 additions & 364 deletions

File tree

RcppTskit/NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ and releases adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
4141
`TableCollection$mutation_table_add_row()` to append mutation rows from
4242
\code{R}, mirroring `tsk_mutation_table_add_row()`.
4343
- Added `rtsk_node_table_get_row()` and `TableCollection$node_table_get_row()`
44-
to retrieve node-table rows by 0-based row ID.
44+
to retrieve node-table rows by 0-based row index.
4545
- Added `rtsk_table_collection_sort()` and `TableCollection$sort()` to sort
4646
table collections with 0-based `edge_start` semantics.
4747
- Added low-level variant iterators

RcppTskit/R/Class-TableCollection.R

Lines changed: 73 additions & 246 deletions
Large diffs are not rendered by default.

RcppTskit/R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ rtsk_node_table_add_row <- function(tc, flags = 0L, time = 0, population = -1L,
243243
.Call(`_RcppTskit_rtsk_node_table_add_row`, tc, flags, time, population, individual, metadata)
244244
}
245245

246-
rtsk_node_table_get_row <- function(tc, row_id) {
247-
.Call(`_RcppTskit_rtsk_node_table_get_row`, tc, row_id)
246+
rtsk_node_table_get_row <- function(tc, index) {
247+
.Call(`_RcppTskit_rtsk_node_table_get_row`, tc, index)
248248
}
249249

250250
rtsk_edge_table_add_row <- function(tc, left, right, parent, child, metadata = NULL) {

RcppTskit/R/RcppTskit.R

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ check_tskit_py <- function(object, stop = FALSE) {
9999
}
100100
}
101101

102+
# INTERNAL
102103
# @title Validating logical args
103104
# @param value logical from the argument
104105
# @param name character of the argument
@@ -109,6 +110,174 @@ validate_logical_arg <- function(value, name) {
109110
}
110111
}
111112

113+
# INTERNAL
114+
# @title Validating integer scalar args
115+
# @param value integer from the argument
116+
# @param name character of the argument
117+
# @param minimum lower bound
118+
# @return No return value; called for side effects.
119+
validate_integer_scalar_arg <- function(value, name, minimum = NULL) {
120+
if (
121+
is.null(value) ||
122+
!is.integer(value) ||
123+
length(value) != 1L ||
124+
is.na(value) ||
125+
(!is.null(minimum) && value < minimum)
126+
) {
127+
if (is.null(minimum)) {
128+
stop(name, " must be a non-NA integer scalar!")
129+
}
130+
if (identical(minimum, 0L)) {
131+
stop(name, " must be a non-NA zero or positive integer scalar!")
132+
}
133+
stop(name, " must be a non-NA integer scalar >= ", minimum, "!")
134+
}
135+
}
136+
137+
# INTERNAL
138+
# @title Validating row indexes
139+
# @param index integer row index (0-based)
140+
# @param name character of the argument
141+
# @param allow_null logical
142+
# @return No return value; called for side effects.
143+
validate_row_index <- function(
144+
index,
145+
name = "index",
146+
allow_null = FALSE
147+
) {
148+
if (allow_null && is.null(index)) {
149+
return(invisible(NULL))
150+
}
151+
validate_integer_scalar_arg(index, name, minimum = 0L)
152+
}
153+
154+
# INTERNAL
155+
# @title Validating optional numeric vectors with no missing values
156+
# @param value numeric vector or \code{NULL}
157+
# @param name character of the argument
158+
# @return No return value; called for side effects.
159+
validate_optional_numeric_vector_arg <- function(value, name) {
160+
if (!is.null(value) && (!is.numeric(value) || anyNA(value))) {
161+
stop(name, " must be NULL or a numeric vector with no NA values!")
162+
}
163+
}
164+
165+
# INTERNAL
166+
# @title Validating optional integer vectors with no missing values
167+
# @param value integer vector or \code{NULL}
168+
# @param name character of the argument
169+
# @return No return value; called for side effects.
170+
validate_optional_integer_vector_arg <- function(value, name) {
171+
if (!is.null(value) && (!is.integer(value) || anyNA(value))) {
172+
stop(name, " must be NULL or an integer vector with no NA values!")
173+
}
174+
}
175+
176+
# INTERNAL
177+
# @title Validating numeric scalar args
178+
# @param value numeric scalar
179+
# @param name character of the argument
180+
# @param allow_null logical
181+
# @param allow_nan logical
182+
# @return No return value; called for side effects.
183+
validate_numeric_scalar_arg <- function(
184+
value,
185+
name,
186+
allow_null = FALSE,
187+
allow_nan = FALSE
188+
) {
189+
if (is.null(value)) {
190+
if (allow_null) {
191+
return(invisible(NULL))
192+
}
193+
stop(name, " must be a non-NA numeric scalar!")
194+
}
195+
if (!is.numeric(value) || length(value) != 1L) {
196+
if (allow_null && allow_nan) {
197+
stop(name, " must be NaN, NULL, or a non-NA numeric scalar!")
198+
}
199+
stop(name, " must be a non-NA numeric scalar!")
200+
}
201+
if (allow_nan) {
202+
if (!is.na(value) || is.nan(value)) {
203+
return(invisible(NULL))
204+
}
205+
stop(name, " must be NaN, NULL, or a non-NA numeric scalar!")
206+
}
207+
if (is.na(value)) {
208+
stop(name, " must be a non-NA numeric scalar!")
209+
}
210+
}
211+
212+
# INTERNAL
213+
# @title Validating character scalar args
214+
# @param value character scalar
215+
# @param name character of the argument
216+
# @return No return value; called for side effects.
217+
validate_character_scalar_arg <- function(value, name) {
218+
if (
219+
is.null(value) ||
220+
!is.character(value) ||
221+
length(value) != 1L ||
222+
is.na(value)
223+
) {
224+
stop(name, " must be a length-1 non-NA character string!")
225+
}
226+
}
227+
228+
# INTERNAL
229+
# @title Validating nullable integer scalar args with sentinel minimum
230+
# @param value integer scalar or \code{NULL}
231+
# @param name character of the argument
232+
# @param minimum integer scalar sentinel minimum
233+
# @return No return value; called for side effects.
234+
validate_nullable_integer_scalar_arg <- function(
235+
value,
236+
name,
237+
minimum = -1L
238+
) {
239+
if (is.null(value)) {
240+
return(invisible(NULL))
241+
}
242+
if (
243+
!is.integer(value) ||
244+
length(value) != 1L ||
245+
is.na(value) ||
246+
value < minimum
247+
) {
248+
stop(
249+
name,
250+
" must be ",
251+
minimum,
252+
", NULL, or a non-NA integer scalar!"
253+
)
254+
}
255+
}
256+
257+
# INTERNAL
258+
# @title Validating metadata argument and possibly converting it to raw
259+
# @param metadata \code{NULL}, character, or raw argument
260+
# @return \code{NULL} when metadata is \code{NULL} and raw vector otherwise.
261+
validate_metadata_arg <- function(metadata) {
262+
if (is.null(metadata)) {
263+
return(NULL)
264+
}
265+
if (
266+
is.character(metadata) &&
267+
length(metadata) == 1L &&
268+
!is.na(metadata)
269+
) {
270+
return(charToRaw(metadata))
271+
}
272+
if (is.raw(metadata)) {
273+
return(metadata)
274+
}
275+
stop(
276+
"metadata must be NULL, a length-1 non-NA character string, or a raw vector!"
277+
)
278+
}
279+
280+
# INTERNAL
112281
# @title Converting load arguments to \code{tskit} bitwise options
113282
# @param skip_tables logical
114283
# @param skip_reference_sequence logical

RcppTskit/inst/include/RcppTskit_public.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int rtsk_individual_table_add_row(
7474
int rtsk_node_table_add_row(
7575
SEXP tc, int flags = 0, double time = 0, int population = -1,
7676
int individual = -1, Rcpp::Nullable<Rcpp::RawVector> metadata = R_NilValue);
77-
Rcpp::List rtsk_node_table_get_row(SEXP tc, int row_id);
77+
Rcpp::List rtsk_node_table_get_row(SEXP tc, int index);
7878
int rtsk_edge_table_add_row(
7979
SEXP tc, double left, double right, int parent, int child,
8080
Rcpp::Nullable<Rcpp::RawVector> metadata = R_NilValue);

0 commit comments

Comments
 (0)