Skip to content

Commit

Permalink
Merge pull request #137 from mrc-ide/mrc-6025
Browse files Browse the repository at this point in the history
Store offset in packing
  • Loading branch information
richfitz authored Nov 21, 2024
2 parents a1aef9d + cd96866 commit 4f02d7c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
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

0 comments on commit 4f02d7c

Please sign in to comment.