Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store offset in packing #137

Merged
merged 10 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dust2
Title: Next Generation dust
Version: 0.3.6
Version: 0.3.7
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "rich.fitzjohn@gmail.com"),
person("Imperial College of Science, Technology and Medicine",
Expand Down
2 changes: 1 addition & 1 deletion R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ dust_template_data <- function(name,
has_adjoint = deparse1(has_adjoint),
parameters = deparse_df(parameters, 4),
default_dt = deparse1(default_dt),
package = paste0(name, mangle %||% ""),
package = paste0(gsub("[^A-Za-z0-9]", ".", name), mangle %||% ""),
linking_to = linking_to,
cpp_std = cpp_std,
compiler_options = compiler_options)
Expand Down
18 changes: 15 additions & 3 deletions inst/include/dust2/packing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ class packing {
using mapping_type = std::pair<std::string, std::vector<size_t>>;
public:
packing(std::initializer_list<mapping_type> data)
: data_(data) {
: data_(data), size_(0) {
len_.reserve(data_.size());
offset_.reserve(data_.size());
for (auto& el : data_) {
len_.push_back(tools::prod(el.second));
const auto len_i = tools::prod(el.second);
len_.push_back(len_i);
offset_.push_back(size_);
size_ += len_i;
}
size_ = std::accumulate(len_.begin(), len_.end(), 0);
}

packing() : size_(0) {
}

auto size() const {
Expand All @@ -34,13 +40,19 @@ class packing {
return data_;
}

template <typename Iter>
void copy_offset(Iter it) {
std::copy(offset_.begin(), offset_.end(), it);
}

bool operator!=(const packing& other) const {
return data_ != other.data();
}

private:
std::vector<mapping_type> data_;
std::vector<size_t> len_;
std::vector<size_t> offset_;
size_t size_;
};

Expand Down
9 changes: 9 additions & 0 deletions inst/include/dust2/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ inline std::vector<size_t> integer_sequence(size_t n) {
return ret;
}

inline std::vector<size_t> integer_sequence(size_t n, size_t offset) {
std::vector<size_t> ret;
ret.reserve(n);
for (size_t i = 0; i < n; ++i) {
ret.push_back(i + offset);
}
return ret;
}

template <typename T>
std::vector<T> subset(const std::vector<T>& x, const std::vector<size_t> index) {
std::vector<T> ret;
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ test_that("can add parameter information", {
})


test_that("can cope with class name containing underscore", {
res <- dust_template_data(
"my_thing", "my_thing", "discrete", FALSE, FALSE, NULL, NULL)
expect_equal(res$class, "my_thing")
expect_equal(res$name, "my_thing")
expect_equal(res$package, "my.thing")
})


test_that("can select optimisation level", {
expect_equal(validate_compiler_options(NULL, NULL), "")
expect_equal(validate_compiler_options("-Xf", NULL), "-Xf")
Expand Down