-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#105 done: new fun: stri_list2matrix
- Loading branch information
Showing
12 changed files
with
270 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
## This file is part of the 'stringi' package for R. | ||
## Copyright (c) 2013-2014, Marek Gagolewski and Bartek Tartanus | ||
## All rights reserved. | ||
## | ||
## Redistribution and use in source and binary forms, with or without | ||
## modification, are permitted provided that the following conditions are met: | ||
## | ||
## 1. Redistributions of source code must retain the above copyright notice, | ||
## this list of conditions and the following disclaimer. | ||
## | ||
## 2. Redistributions in binary form must reproduce the above copyright notice, | ||
## this list of conditions and the following disclaimer in the documentation | ||
## and/or other materials provided with the distribution. | ||
## | ||
## 3. Neither the name of the copyright holder nor the names of its | ||
## contributors may be used to endorse or promote products derived from | ||
## this software without specific prior written permission. | ||
## | ||
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
## BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
## FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
## HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
#' @title | ||
#' Convert a List to a Character Matrix | ||
#' | ||
#' @description | ||
#' This function converts a given list of atomic vectors to | ||
#' a character matrix. | ||
#' | ||
#' @details | ||
#' This function is similar to the builtin \code{\link{simplify2array}} | ||
#' function. However, it always returns a character matrix, | ||
#' even if each element in \code{x} is of length 1 | ||
#' or if elements in \code{x} are not of the same lenghts. | ||
#' Moreover, the elements in \code{x} are always coerced to character vectors. | ||
#' | ||
#' If \code{byrow} is \code{FALSE}, then a matrix with \code{length(x)} | ||
#' columns is returned. The number of rows is the lenght of the | ||
#' longest vector in \code{x}. Basically, we have | ||
#' \code{result[i,j] == x[[j]][i]} if \code{i <= length(x[[j]])} | ||
#' and \code{result[i,j] == fill} otherwise, see Examples. | ||
#' | ||
#' If \code{byrow} is \code{TRUE}, then the resulting matrix is | ||
#' a transposition of the above-described one. | ||
#' | ||
#' This function may be useful e.g. in connection with \code{\link{stri_split}}. | ||
#' | ||
#' @param x a list of atomic vectors | ||
#' @param byrow single logical value; should the resulting matrix be | ||
#' transposed? | ||
#' @param fill single string, see Details | ||
#' | ||
#' @return | ||
#' Always returns a character matrix. | ||
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' simplify2array(list(c("a", "b"), c("c", "d"), c("e", "f"))) | ||
#' stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f"))) | ||
#' stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f")), byrow=TRUE) | ||
#' | ||
#' simplify2array(list("a", c("b", "c"))) | ||
#' stri_list2matrix(list("a", c("b", "c"))) | ||
#' stri_list2matrix(list("a", c("b", "c")), fill="") | ||
#' } | ||
#' | ||
#' @family utils | ||
#' @export | ||
stri_list2matrix <- function(x, byrow=FALSE, fill=NA_character_) { | ||
.Call("stri_list2matrix", x, byrow, stri_enc_toutf8(fill), PACKAGE="stringi") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require(testthat) | ||
|
||
test_that("stri_list2matrix", { | ||
|
||
expect_identical(stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f"))), | ||
simplify2array(list(c("a", "b"), c("c", "d"), c("e", "f")))) | ||
|
||
expect_identical(stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f")), byrow=TRUE), | ||
t(simplify2array(list(c("a", "b"), c("c", "d"), c("e", "f"))))) | ||
|
||
expect_identical(stri_list2matrix(list("a", c("b", "c"))), | ||
matrix(c("a", NA, "b", "c"), ncol=2)) | ||
|
||
expect_identical(stri_list2matrix(list("a", c("b", "c")), fill=""), | ||
matrix(c("a", "", "b", "c"), ncol=2)) | ||
|
||
expect_identical(stri_list2matrix(list()), structure(character(0), dim=c(0,0))) | ||
expect_identical(stri_list2matrix(list(character(0), character(0))), structure(character(0), dim=c(0,2))) | ||
expect_error(stri_list2matrix(list(LETTERS, mean, letters))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
% Generated by roxygen2 (4.0.2): do not edit by hand | ||
\name{stri_list2matrix} | ||
\alias{stri_list2matrix} | ||
\title{Convert a List to a Character Matrix} | ||
\usage{ | ||
stri_list2matrix(x, byrow = FALSE, fill = NA_character_) | ||
} | ||
\arguments{ | ||
\item{x}{a list of atomic vectors} | ||
|
||
\item{byrow}{single logical value; should the resulting matrix be | ||
transposed?} | ||
|
||
\item{fill}{single string, see Details} | ||
} | ||
\value{ | ||
Always returns a character matrix. | ||
} | ||
\description{ | ||
This function converts a given list of atomic vectors to | ||
a character matrix. | ||
} | ||
\details{ | ||
This function is similar to the builtin \code{\link{simplify2array}} | ||
function. However, it always returns a character matrix, | ||
even if each element in \code{x} is of length 1 | ||
or if elements in \code{x} are not of the same lenghts. | ||
Moreover, the elements in \code{x} are always coerced to character vectors. | ||
|
||
If \code{byrow} is \code{FALSE}, then a matrix with \code{length(x)} | ||
columns is returned. The number of rows is the lenght of the | ||
longest vector in \code{x}. Basically, we have | ||
\code{result[i,j] == x[[j]][i]} if \code{i <= length(x[[j]])} | ||
and \code{result[i,j] == fill} otherwise, see Examples. | ||
|
||
If \code{byrow} is \code{TRUE}, then the resulting matrix is | ||
a transposition of the above-described one. | ||
|
||
This function may be useful e.g. in connection with \code{\link{stri_split}}. | ||
} | ||
\examples{ | ||
\donttest{ | ||
simplify2array(list(c("a", "b"), c("c", "d"), c("e", "f"))) | ||
stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f"))) | ||
stri_list2matrix(list(c("a", "b"), c("c", "d"), c("e", "f")), byrow=TRUE) | ||
|
||
simplify2array(list("a", c("b", "c"))) | ||
stri_list2matrix(list("a", c("b", "c"))) | ||
stri_list2matrix(list("a", c("b", "c")), fill="") | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,4 +72,5 @@ stri_trans_transliterate.cpp \ | |
stri_uchar.cpp \ | ||
stri_ucnv.cpp \ | ||
stri_uloc.cpp \ | ||
stri_utils.cpp \ | ||
stri_wrap.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* This file is part of the 'stringi' package for R. | ||
* Copyright (c) 2013-2014, Marek Gagolewski and Bartek Tartanus | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
|
||
#include "stri_stringi.h" | ||
#include "stri_container_utf8.h" | ||
#include "stri_container_listutf8.h" | ||
|
||
|
||
/** | ||
* Convert list to a character matrix | ||
* | ||
* @param x a list | ||
* @param fill single string | ||
* @param byrow single logical value | ||
* @return character matrix | ||
* | ||
* @version 0.3-1 (Marek Gagolewski, 2014-10-23) | ||
*/ | ||
SEXP stri_list2matrix(SEXP x, SEXP byrow, SEXP fill) | ||
{ | ||
x = stri_prepare_arg_list_string(x, "x"); | ||
bool byrow2 = stri__prepare_arg_logical_1_notNA(byrow, "byrow"); | ||
fill = stri_prepare_arg_string_1(fill, "fill"); // enc2utf8 called in R | ||
|
||
STRI__ERROR_HANDLER_BEGIN | ||
R_len_t n = LENGTH(x); | ||
SEXP fill2 = STRING_ELT(fill, 0); | ||
|
||
R_len_t m = 0; // maximal vector length | ||
for (int i=0; i<n; ++i) { | ||
R_len_t k = LENGTH(VECTOR_ELT(x, i)); | ||
if (k > m) m = k; | ||
} | ||
|
||
SEXP ret; | ||
if (!byrow2) { | ||
STRI__PROTECT(ret = Rf_allocMatrix(STRSXP, m, n)); | ||
int ret_idx = 0; | ||
for (int i=0; i<n; ++i) { | ||
SEXP cur_str = VECTOR_ELT(x, i); | ||
R_len_t cur_len = LENGTH(cur_str); | ||
int j; | ||
for (j=0; j<cur_len; ++j) | ||
SET_STRING_ELT(ret, ret_idx++, STRING_ELT(cur_str, j)); | ||
for (; j<m; ++j) | ||
SET_STRING_ELT(ret, ret_idx++, fill2); | ||
} | ||
} | ||
else { | ||
STRI__PROTECT(ret = Rf_allocMatrix(STRSXP, n, m)); | ||
for (int i=0; i<n; ++i) { | ||
SEXP cur_str = VECTOR_ELT(x, i); | ||
R_len_t cur_len = LENGTH(cur_str); | ||
int j; | ||
for (j=0; j<cur_len; ++j) | ||
SET_STRING_ELT(ret, i+j*n, STRING_ELT(cur_str, j)); | ||
for (; j<m; ++j) | ||
SET_STRING_ELT(ret, i+j*n, fill2); | ||
} | ||
} | ||
|
||
STRI__UNPROTECT_ALL | ||
return ret; | ||
|
||
STRI__ERROR_HANDLER_END({/* no-op on err */}) | ||
} | ||
|