From ca7bcfca6d16e6d09b2314a158702af6d2acaff5 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Mon, 18 Nov 2024 18:26:00 +0000 Subject: [PATCH 01/10] Store offset in packing --- inst/include/dust2/packing.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/inst/include/dust2/packing.hpp b/inst/include/dust2/packing.hpp index 0d1d2bfe..dc68cd76 100644 --- a/inst/include/dust2/packing.hpp +++ b/inst/include/dust2/packing.hpp @@ -14,10 +14,14 @@ class packing { using mapping_type = std::pair>; public: packing(std::initializer_list 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); } @@ -34,6 +38,10 @@ class packing { return data_; } + auto& offset() const { + return offset_; + } + bool operator!=(const packing& other) const { return data_ != other.data(); } @@ -41,6 +49,7 @@ class packing { private: std::vector data_; std::vector len_; + std::vector offset_; size_t size_; }; From 5b352ae808ec1673e4d1054f4a8b57f98e8c4c8d Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Tue, 19 Nov 2024 20:46:37 +0000 Subject: [PATCH 02/10] Create valid R package names --- R/compile.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/compile.R b/R/compile.R index 9555c47c..4c0487b8 100644 --- a/R/compile.R +++ b/R/compile.R @@ -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-z1-9]", ".", name), mangle %||% ""), linking_to = linking_to, cpp_std = cpp_std, compiler_options = compiler_options) From 3b1d51e80f4978cd6ef287c7a04073bb2a3b0c52 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Tue, 19 Nov 2024 20:47:02 +0000 Subject: [PATCH 03/10] Add default constructor for packing --- inst/include/dust2/packing.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inst/include/dust2/packing.hpp b/inst/include/dust2/packing.hpp index dc68cd76..824e9958 100644 --- a/inst/include/dust2/packing.hpp +++ b/inst/include/dust2/packing.hpp @@ -26,6 +26,9 @@ class packing { size_ = std::accumulate(len_.begin(), len_.end(), 0); } + packing() { + } + auto size() const { return size_; } From ce63955d25c186e23229ec07fce74a8002e48846 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Tue, 19 Nov 2024 20:47:15 +0000 Subject: [PATCH 04/10] Utility to copy offset --- inst/include/dust2/packing.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inst/include/dust2/packing.hpp b/inst/include/dust2/packing.hpp index 824e9958..168b0c8f 100644 --- a/inst/include/dust2/packing.hpp +++ b/inst/include/dust2/packing.hpp @@ -45,6 +45,11 @@ class packing { return offset_; } + template + void copy_offset(Iter it) { + std::copy(offset_.begin(), offset_.end(), it); + } + bool operator!=(const packing& other) const { return data_ != other.data(); } From 6f8842ca143ba4327b984c80dbd6a197b4120d65 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Tue, 19 Nov 2024 20:47:31 +0000 Subject: [PATCH 05/10] More general integer sequence --- inst/include/dust2/tools.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/inst/include/dust2/tools.hpp b/inst/include/dust2/tools.hpp index 75b16381..415db41f 100644 --- a/inst/include/dust2/tools.hpp +++ b/inst/include/dust2/tools.hpp @@ -61,6 +61,15 @@ inline std::vector integer_sequence(size_t n) { return ret; } +inline std::vector integer_sequence(size_t n, size_t offset) { + std::vector ret; + ret.reserve(n); + for (size_t i = 0; i < n; ++i) { + ret.push_back(i + offset); + } + return ret; +} + template std::vector subset(const std::vector& x, const std::vector index) { std::vector ret; From f9ba185d614f02dd3063f55f3463e328f743fa38 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Tue, 19 Nov 2024 21:04:18 +0000 Subject: [PATCH 06/10] Zero size on creation --- inst/include/dust2/packing.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/dust2/packing.hpp b/inst/include/dust2/packing.hpp index 168b0c8f..b8cc3c35 100644 --- a/inst/include/dust2/packing.hpp +++ b/inst/include/dust2/packing.hpp @@ -26,7 +26,7 @@ class packing { size_ = std::accumulate(len_.begin(), len_.end(), 0); } - packing() { + packing() : size_(0) { } auto size() const { From 22f700baeacf462621eba3878935061cf79e4544 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 21 Nov 2024 18:48:33 +0000 Subject: [PATCH 07/10] Drop unused bits --- inst/include/dust2/packing.hpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/inst/include/dust2/packing.hpp b/inst/include/dust2/packing.hpp index b8cc3c35..ef12f99c 100644 --- a/inst/include/dust2/packing.hpp +++ b/inst/include/dust2/packing.hpp @@ -23,7 +23,6 @@ class packing { offset_.push_back(size_); size_ += len_i; } - size_ = std::accumulate(len_.begin(), len_.end(), 0); } packing() : size_(0) { @@ -41,10 +40,6 @@ class packing { return data_; } - auto& offset() const { - return offset_; - } - template void copy_offset(Iter it) { std::copy(offset_.begin(), offset_.end(), it); From bfb3410058747c159001de5d32d0b3788eabc5d9 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 21 Nov 2024 18:51:59 +0000 Subject: [PATCH 08/10] Add test --- tests/testthat/test-compile.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test-compile.R b/tests/testthat/test-compile.R index de4a9d0b..671ba676 100644 --- a/tests/testthat/test-compile.R +++ b/tests/testthat/test-compile.R @@ -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") From de4496b0d029d39630c39be1ae81d8b1c0c1053d Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 21 Nov 2024 18:53:20 +0000 Subject: [PATCH 09/10] Bump version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6170253f..c3d6137a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", From cd968666438d21d19520a5cc0cf662268f06162b Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 21 Nov 2024 21:56:07 +0000 Subject: [PATCH 10/10] Fix regex --- R/compile.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/compile.R b/R/compile.R index 4c0487b8..198f309f 100644 --- a/R/compile.R +++ b/R/compile.R @@ -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(gsub("[^A-Za-z1-9]", ".", name), mangle %||% ""), + package = paste0(gsub("[^A-Za-z0-9]", ".", name), mangle %||% ""), linking_to = linking_to, cpp_std = cpp_std, compiler_options = compiler_options)