Add methods `fill()` and `imbue()`.

This commit is contained in:
CismonX 2019-07-24 00:55:42 +08:00
parent 672508bb38
commit 38aab4343a
2 changed files with 71 additions and 2 deletions

View File

@ -80,13 +80,44 @@ namespace php_arma
template <typename T, typename T1>
PHP_ARMA_METHOD(dense, fill, T, T1)
{
zval *value;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(value);
ZEND_PARSE_PARAMETERS_END();
if (!zval_check_scalar<T>(value)) {
return;
}
auto native = THIS_NATIVE;
native->fill(zval_get_scalar<T>(value));
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense, imbue, T, T1)
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_FUNC(fci, fcc);
ZEND_PARSE_PARAMETERS_END();
zval retval;
fci.retval = &retval;
auto native = THIS_NATIVE;
native->imbue([&fci, &fcc]() {
zend_call_function(&fci, &fcc);
if (!zval_check_scalar<T>(fci.retval)) {
zval_ptr_dtor(fci.retval);
return T();
}
T retval = zval_get_scalar<T>(fci.retval);
if constexpr (std::is_same_v<T, cx_double>) {
zval_ptr_dtor(fci.retval);
}
return retval;
});
}
template <typename T, typename T1>

38
tests/011-fill-imbue.phpt Normal file
View File

@ -0,0 +1,38 @@
--TEST--
Test for methods `fill()` and `imbue()`.
--SKIPIF--
<?php
require_once 'includes/loaded.php';
is_php_arma_loaded();
?>
--FILE--
<?php
require_once 'includes/assert.php';
$mat = Arma\IMat::init(2, 2, Arma\Fill::ZEROS);
$mat->fill(5);
batch_assert('method `fill()`',
[5, $mat->max()],
[5, $mat->min()]
);
$cnt = 0;
$mat1 = Arma\IMat::fromArray([
[1, 3],
[2, 4]
]);
$mat->imbue(function () use (&$cnt) {
return ++$cnt;
});
$comp = function ($expected, $got) {
$compare_result = $expected->equals($got);
return $compare_result->max() == 1 && $compare_result->min() == 1;
};
batch_assert_ex('method `imbue()`', $comp,
[$mat1, $mat]
);
?>
--EXPECT--