Skip to content

Commit

Permalink
refactor: use is_na and some simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gisler committed Jan 4, 2024
1 parent 5860ba0 commit bc3fd8c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
11 changes: 3 additions & 8 deletions src/D8slope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ arma::dmat D8slope(
const double ns_fpl,
const int is_ths = 1
) {
/* With integers, missing values are stored as the smallest integer
* (-2.147.483.648). See also https://adv-r.hadley.nz/rcpp.html
*/
const int NA_integer_ = Rcpp::IntegerVector::get_na();

arma::dmat nm_dem_pad(
arma::size(nm_dem) + 2,
arma::fill::value(Rcpp::NumericVector::get_na())
arma::fill::value(NA_REAL)
);
nm_dem_pad(1, 1, arma::size(nm_dem)) = nm_dem;

Expand All @@ -28,13 +23,13 @@ arma::dmat D8slope(

arma::dmat nm_slp(
arma::size(nm_dem),
arma::fill::value(Rcpp::NumericVector::get_na())
arma::fill::value(NA_REAL)
);

#pragma omp parallel for num_threads(is_ths) collapse(2)
for (arma::uword i = 1; i < nm_dem_pad.n_rows - 1; ++i) {
for (arma::uword j = 1; j < nm_dem_pad.n_cols - 1; ++j) {
if (im_dir.at(i - 1, j - 1) == NA_integer_) {
if (Rcpp::IntegerMatrix::is_na(im_dir.at(i - 1, j - 1))) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Makevars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXX_STD = CXX17

PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
2 changes: 1 addition & 1 deletion src/Makevars.win
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXX_STD = CXX17

PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
15 changes: 5 additions & 10 deletions src/dir_sth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ arma::imat dir_sth(
const arma::imat& im_fDo,
const int is_ths = 1
) {
/* With integers, missing values are stored as the smallest integer
* (-2.147.483.648). See also https://adv-r.hadley.nz/rcpp.html
*/
const int NA_integer_ = Rcpp::IntegerVector::get_na();

arma::imat im_sth_pad(arma::size(im_sth) + 2, arma::fill::value(NA_integer_));
arma::imat im_sth_pad(arma::size(im_sth) + 2, arma::fill::value(NA_INTEGER));
im_sth_pad(1, 1, arma::size(im_sth)) = im_sth;

int is_sth = {1};
arma::imat im_xxx(arma::size(im_sth), arma::fill::value(NA_integer_));
arma::imat im_xxx(arma::size(im_sth), arma::fill::value(NA_INTEGER));

#pragma omp parallel for num_threads(is_ths) collapse(2)
for (arma::uword i = 1; i < im_sth_pad.n_rows - 1; ++i) {
for (arma::uword j = 1; j < im_sth_pad.n_cols - 1; ++j) {
if (im_dir.at(i - 1, j - 1) == NA_integer_ ||
im_sth.at(i - 1, j - 1) != NA_integer_) {
if ( Rcpp::IntegerMatrix::is_na(im_dir.at(i - 1, j - 1)) ||
!Rcpp::IntegerMatrix::is_na(im_sth.at(i - 1, j - 1))) {
continue;
}

Expand All @@ -36,7 +31,7 @@ arma::imat dir_sth(
)
);

if (is_tmp != NA_integer_ && is_tmp != 0) {
if (!Rcpp::IntegerMatrix::is_na(is_tmp) && is_tmp != 0) {
im_xxx.at(i - 1, j - 1) = is_sth;
++is_sth;
}
Expand Down

0 comments on commit bc3fd8c

Please sign in to comment.