implement `forEach()`

This commit is contained in:
CismonX 2019-08-12 02:15:06 +08:00
parent 10913b6f7d
commit 0db6308019
2 changed files with 48 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "base.hh"
#include "mat.hh"
#include "subview_mat.hh"
#include "mapval.hh"
#include "stream_utils.hh"
#include <armadillo>
@ -73,7 +74,24 @@ namespace php_arma
template <typename T, typename T1>
PHP_ARMA_METHOD(base, forEach, 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 params[1], retval;
fci.param_count = 1;
fci.params = params;
fci.retval = &retval;
auto native = THIS_NATIVE;
native->for_each([&fci, &fcc](T& val) {
ZVAL_OBJ(&fci.params[0], mapval_dense<T>::create(&val));
zend_call_function(&fci, &fcc);
zval_ptr_dtor(&fci.params[0]);
zval_ptr_dtor(fci.retval);
});
}
template <typename T, typename T1>

29
tests/013-foreach.phpt Normal file
View File

@ -0,0 +1,29 @@
--TEST--
Test for method `forEach()`.
--SKIPIF--
<?php
require_once 'includes/loaded.php';
is_php_arma_loaded();
?>
--FILE--
<?php
require_once 'includes/assert.php';
$mat = Arma\IMat::fromString('1 2; 3 4');
$mat->rawPrint();
$mat->forEach(function (Arma\MapVal $elem) {
$elem_val = $elem->val();
echo $elem_val;
$elem->setTo($elem_val + 4);
});
echo PHP_EOL;
$mat->rawPrint();
?>
--EXPECT--
1 2
3 4
1324
5 6
7 8