diff --git a/inst/include/pmt/progress.hpp b/inst/include/pmt/progress.hpp index 9daf484d..09aec99c 100644 --- a/inst/include/pmt/progress.hpp +++ b/inst/include/pmt/progress.hpp @@ -34,48 +34,39 @@ constexpr auto generate_bars(std::integer_sequence) constexpr auto generated_bars = generate_bars(std::make_integer_sequence()); -template -class PermuBarBase { -private: - T& _Derived() - { - return static_cast(*this); - } - +class PermuBarHide { public: - template - void init(R_xlen_t n_permu, U update_lambda, R_len_t statistic_size = 1) + template + void init(R_xlen_t n_permu, T update, R_len_t statistic_size = 1) { _init_statistic_buffer(statistic_size, 1); - update_lambda(); + update(); _statistic = _statistic_buffer; - _Derived().init_impl(n_permu, statistic_size); + _init_statistic_buffer(n_permu, statistic_size); } bool operator<<(double statistic) { - return _Derived().update_impl(statistic); - } + _statistic_buffer[_buffer_i++] = statistic; - bool operator<<(SEXP statistic) - { - return _Derived().update_impl(as(statistic)); + return _buffer_i != _buffer_size; } NumericVector close() { _statistic.attr("permu") = _statistic_buffer; - return _Derived().close_impl(); + return _statistic; } protected: - NumericVector _statistic; - R_xlen_t _buffer_i; R_xlen_t _buffer_size; +private: + NumericVector _statistic; + NumericVector _statistic_buffer; void _init_statistic_buffer(R_xlen_t n_statistic, R_len_t statistic_size) @@ -89,67 +80,44 @@ class PermuBarBase { _statistic_buffer.attr("dim") = IntegerVector::create(statistic_size, n_statistic); } } - - bool _update_double(double statistic) - { - _statistic_buffer[_buffer_i++] = statistic; - - return _buffer_i != _buffer_size; - } -}; - -class PermuBarHide : public PermuBarBase { -public: - void init_impl(R_xlen_t n_permu, R_len_t statistic_size) - { - _init_statistic_buffer(n_permu, statistic_size); - } - - bool update_impl(double statistic) - { - return _update_double(statistic); - } - - NumericVector close_impl() - { - return _statistic; - } }; -class PermuBarShow : public PermuBarBase { +class PermuBarShow : public PermuBarHide { public: - void init_impl(R_xlen_t n_permu, R_len_t statistic_size) + template + auto init(Args&&... args) { - _init_statistic_buffer(n_permu, statistic_size); + PermuBarHide::init(std::forward(args)...); - _update_i = 0; - _update_every = (_buffer_size < 100) ? 1 : _buffer_size / 100; + _show_i = 0; + _show_every = (_buffer_size < 100) ? 1 : _buffer_size / 100; - _print(); + _show(); } - bool update_impl(double statistic) + template + auto operator<<(Args&&... args) { - if (++_update_i == _update_every) { - _update_i = 0; - _print(); + if (++_show_i == _show_every) { + _show_i = 0; + _show(); } - return _update_double(statistic); + return PermuBarHide::operator<<(std::forward(args)...); } - NumericVector close_impl() + auto close() { Rcout << "\015\033[K\033[0m"; - return _statistic; + return PermuBarHide::close(); } private: - R_xlen_t _update_i = 0; - R_xlen_t _update_every = 2; + R_xlen_t _show_i = 0; + R_xlen_t _show_every = 2; - void _print() + void _show() { unsigned percent = static_cast(100 * _buffer_i / _buffer_size); diff --git a/src/pmt_interface.cpp b/src/pmt_interface.cpp index 4b021e98..15806459 100644 --- a/src/pmt_interface.cpp +++ b/src/pmt_interface.cpp @@ -15,10 +15,13 @@ class ClosFunc { _func(func) { } template - Function operator()(Args&&... args) const + auto operator()(Args... args) const { - Function closure = _func(std::forward(args)...); - return closure; + Function closure = _func(args...); + + return [closure](auto... args_) { + return as(closure(args_...)); + }; } };