-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b1318d
commit 5929386
Showing
1 changed file
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <Rcpp.h> | ||
#include <unordered_set> | ||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
IntegerVector count_unique_landuse(NumericVector x, size_t ni, size_t nw) { | ||
IntegerVector out(ni); | ||
|
||
// loop over cells | ||
size_t start = 0; | ||
for (size_t i = 0; i < ni; i++) { | ||
size_t end = start + nw; | ||
|
||
// Use an unordered_set to automatically handle uniqueness | ||
std::unordered_set<int> unique_values; | ||
bool all_na = true; | ||
|
||
// loop over the values of a window | ||
for (size_t j = start; j < end; j++) { | ||
if (!R_IsNA(x[j])) { | ||
// insert() only adds the value if it's not already in the set | ||
unique_values.insert(static_cast<int>(x[j])); | ||
all_na = false; | ||
} | ||
} | ||
|
||
// If all values in the window are NA, set the output to NA | ||
// Otherwise, set it to the count of unique values | ||
if (all_na) { | ||
out[i] = NA_INTEGER; | ||
} else { | ||
out[i] = unique_values.size(); | ||
} | ||
|
||
start = end; | ||
} | ||
|
||
return out; | ||
} |