Skip to content

Commit

Permalink
nswitch (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
2005m authored Nov 19, 2020
1 parent d75c09b commit 8807526
Show file tree
Hide file tree
Showing 10 changed files with 917 additions and 34 deletions.
17 changes: 9 additions & 8 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
6071edd604dbeb75308cfbedc7790398 *cleanup
30a444c58adaf9b99783d22d75d60c61 *configure
2f5c53949e9b803d6a51f29066966f70 *DESCRIPTION
d35ef43a14f3f264d1637211ea8c344d *inst/NEWS.Rd
9cf93298326a8dbf32ac14957ceb2114 *inst/NEWS.Rd
a87b0f223435ed35607e8514562b8bfe *LICENSE
8f19a2c9feb2f352580fd4892650f285 *man/count.Rd
d143e1321d840bb810d9648e54e22c9c *man/fpos.Rd
Expand All @@ -11,21 +11,22 @@ d143e1321d840bb810d9648e54e22c9c *man/fpos.Rd
642f1b417705a95de829d995fecf476b *man/psum.Rd
14e3add07190fd18d0161f4f0b0b5257 *man/setlevels.Rd
e0d3b091b5182fdcc8868c93a2c59476 *man/topn.Rd
dc9e6e1fbeb0c05694f566970bc57b10 *man/vswitch.Rd
8b041db1ae628bd8241f5e57db318669 *NAMESPACE
993fb0fcb67daa7df018344f795635c6 *R/call.R
3c628c2a27764ec5df2b4980921c310f *man/vswitch.Rd
5cbe08d5cbbf4968690f18c413ee2a38 *NAMESPACE
87b9a4f49893ed5f170f79d15f35fc56 *R/call.R
fa0ca12018176564236efd16b8d8e9d8 *README.md
9c2779934039f6de5758068f78084327 *src/dup.c
f6e01fe007c5384668165c0efcc159ff *src/dupLen.c
84230b3325b0d1b661e4688f9aac670a *src/fpos.c
d800a56f0076c3d2aa747ee02653f0d2 *src/iif.c
e7f79b9871ca0c5b93d652713982e7ec *src/init.c
36c0f5e0615a12b304734d987adde491 *src/kit.h
b6d184807563eb8d74a8586c598ed770 *src/init.c
cdcf7d1759e07f4079b8664f7e3b8192 *src/kit.h
63b898312ccf39a20bd7fbd8dcca5621 *src/Makevars.in
95e3011e37d9dde0d75f3a3819b2acd3 *src/Makevars.win
1009d8311fef48e50b93d47cb3b78824 *src/nswitch.c
d2f4fdbd388ae96944e5de4e7c8fb138 *src/psum.c
91d603f722355667800428c1a6c03d34 *src/topn.c
7594787e656e81a952602afa1711405b *src/utils.c
403cced156980d4dab25d16d67dadb61 *src/vswitch.c
c197e3aab79d9d8902cf1f43a234ffda *tests/test_kit.R
5cb347ede99a3fb9b75f3d14257ea9cf *tests/test_kit.Rout.save
91754347b8161456adf8b6f041369a12 *tests/test_kit.R
c6647d9b92f372eb1590e0df8e675129 *tests/test_kit.Rout.save
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
useDynLib(kit, .registration = TRUE)

export(
count, countNA, countOccur, fduplicated, fpos, funique, iif, nif, pall,
count, countNA, countOccur, fduplicated, fpos, funique, iif, nif, nswitch, pall,
pany, pcount, pmean, pprod, psum, setlevels, topn, uniqLen, vswitch
)
3 changes: 2 additions & 1 deletion R/call.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ fpos = function(needle, haystack, all=TRUE, overlap=TRUE) .Call(CfposR, n
funique = function(x) .Call(CdupR, x, TRUE)
iif = function(test, yes, no, na=NULL, tprom=FALSE, nThread=getOption("kit.nThread")) .Call(CiifR, test, yes, no, na, tprom, nThread)
nif = function(..., default=NULL) .Call(CnifR, default, parent.frame(), as.list(substitute(...())))
pany = function(..., na.rm=FALSE) .Call(CpanyR, na.rm, list(...))
nswitch = function(x, ..., default=NULL, nThread=getOption("kit.nThread"), checkEnc = TRUE) .Call(CnswitchR, x, default, nThread, checkEnc, list(...))
pall = function(..., na.rm=FALSE) .Call(CpallR, na.rm, list(...))
pany = function(..., na.rm=FALSE) .Call(CpanyR, na.rm, list(...))
pcount = function(..., value) .Call(CpcountR, value, list(...))
pmean = function(..., na.rm=FALSE) .Call(CpmeanR, na.rm, list(...))
pprod = function(..., na.rm=FALSE) .Call(CpprodR, na.rm, list(...))
Expand Down
6 changes: 5 additions & 1 deletion inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
vectors, \code{data.frame} and \code{matrix}. It should be faster than these functions. For more
details and benchmark please see \code{?kit::uniqLen}.

\item Function \code{\strong{switch}} now supports mixed encoding and gains an additional argument
\item Function \code{\strong{vswitch}} now supports mixed encoding and gains an additional argument
\code{checkEnc=TRUE}. Thanks to Xianying Tan (@shrektan) for the request and review
\href{https://github.com/2005m/kit/pull/7}{PR#7}.

\item Function \code{\strong{nswitch}} is a nested version of function \code{\strong{vswitch}}
and also supports mixed encoding. Please see please see \code{?kit::nswitch} for further details.
Thanks to Xianying Tan (@shrektan) for the request and review \href{https://github.com/2005m/kit/pull/10}{PR#10}.
}
}
\subsection{Notes}{
Expand Down
40 changes: 32 additions & 8 deletions man/vswitch.Rd
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
\name{vswitch}
\name{vswitch/nswitch}
\alias{vswitch}
\alias{nswitch}
\title{ Vectorised switch }
\description{
\code{vswitch} is a vectorised version of \code{base} function \code{switch}. This function can also be seen as a particular case of function \code{nif}, as shown in examples below, and should also be faster.
\code{vswitch}/ \code{nswitch} is a vectorised version of \code{base} function \code{switch}. This function can also be seen as a particular case of function \code{nif}, as shown in examples below, and should also be faster.
}
\usage{
vswitch(x, values, outputs, default=NULL,
nThread=getOption("kit.nThread"),
checkEnc=TRUE)
nswitch(x, ..., default=NULL,
nThread=getOption("kit.nThread"),
checkEnc=TRUE)
}
\arguments{
\item{x}{A vector or list.}
\item{values}{A vector or list with values from \code{x} to match. Note that \code{x} and \code{values} must have the same class and attributes.}
\item{outputs}{A list or vector with the outputs to return for every matching values. Each item of the list must be of length 1 or length of \code{x}. Note that if all list items are of length 1 then it might be simpler to use a vector.}
\item{...}{A sequence of values and outputs in the following order \code{value1, output1, value2, output2, ..., valueN, outputN}. Values \code{value1, value2, ..., valueN} must all have length1, same type and attributes. Each \code{output} may either share length with \code{x} or be length 1. Please see Examples section for further details.}
\item{default}{Values to return is no match. Must be a vector or list of length 1 or same length as \code{x}. Also, \code{default} must have the same type, class and aatributes as items from \code{outputs}.}
\item{nThread}{ A integer for the number of threads to use with \emph{openmp}. Default value is \code{getOption("kit.nThread")}.}
\item{checkEnc}{ A logical value whether or not to check if \code{x} and \code{values} have comparable and consistant encoding. Default is \code{TRUE}.}
Expand All @@ -29,7 +34,7 @@
x = sample(c(10L, 20L, 30L, 40L, 50L, 60L), 3e2, replace=TRUE)

# The below example of 'vswitch' is
a = vswitch(
a1 = vswitch(
x = x,
values = c(10L,20L,30L,40L,50L),
outputs = c(11L,21L,31L,41L,51L),
Expand All @@ -38,32 +43,51 @@ a = vswitch(

# equivalent to the following 'nif' example.
# However for large vectors 'vswitch' should be faster.
b = nif(
b1 = nif(
x==10L, 11L,
x==20L, 21L,
x==30L, 31L,
x==40L, 41L,
x==50L, 51L,
default = NA_integer_
)
identical(a, b)
identical(a1, b1)

# nswitch can also be used as follows:
c1 = nswitch(x,
10L, 11L,
20L, 21L,
30L, 31L,
40L, 41L,
50L, 51L,
default = NA_integer_
)
identical(a1, c1)

# Example with list in 'outputs' argument
y = c(1, 0, NA_real_)
c = vswitch(
a2 = vswitch(
x = y,
values = c(1, 0),
outputs = list(c(2, 3, 4), c(5, 6, 7)),
default = 8
)

d = nif(
b2 = nif(
y==1, c(2, 3, 4),
y==0, c(5, 6, 7),
default = 8
)

identical(c, d)
identical(a2, b2)

c2 = nswitch(y,
1, c(2, 3, 4),
0, c(5, 6, 7),
default = 8
)

identical(a2, c2)

# Benchmarks
# ----------
Expand Down
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static const R_CallMethodDef CallEntries[] = {
{"CfposR", (DL_FUNC) &fposR, -1},
{"CiifR", (DL_FUNC) &iifR, -1},
{"CnifR", (DL_FUNC) &nifR, -1},
{"CnswitchR", (DL_FUNC) &nswitchR, -1},
{"CompEnabledR", (DL_FUNC) &ompEnabledR, -1},
{"CpanyR", (DL_FUNC) &panyR, -1},
{"CpallR", (DL_FUNC) &pallR, -1},
Expand All @@ -33,6 +34,7 @@ void R_init_kit(DllInfo *dll) {
R_RegisterCCallable("kit", "CfposR", (DL_FUNC) &fposR);
R_RegisterCCallable("kit", "CiifR", (DL_FUNC) &iifR);
R_RegisterCCallable("kit", "CnifR", (DL_FUNC) &nifR);
R_RegisterCCallable("kit", "CnswitchR", (DL_FUNC) &nswitchR);
R_RegisterCCallable("kit", "CpanyR", (DL_FUNC) &panyR);
R_RegisterCCallable("kit", "CpallR", (DL_FUNC) &pallR);
R_RegisterCCallable("kit", "CpcountR", (DL_FUNC) &pcountR);
Expand Down
1 change: 1 addition & 0 deletions src/kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extern SEXP fposVectR(SEXP ndle, SEXP hsk, SEXP all, SEXP overlap);
extern SEXP iifR(SEXP l, SEXP a, SEXP b, SEXP na, SEXP tprom, SEXP nthreads);
extern SEXP nifR(SEXP na, SEXP rho, SEXP args);
extern SEXP nifInternalR(SEXP na, SEXP rho, SEXP args);
extern SEXP nswitchR(SEXP x, SEXP na, SEXP nthreads, SEXP chkenc, SEXP args);
extern SEXP ompEnabledR();
extern SEXP pallR(SEXP na, SEXP args);
extern SEXP panyR(SEXP na, SEXP args);
Expand Down
Loading

0 comments on commit 8807526

Please sign in to comment.