refactor && implementation

This commit is contained in:
CismonX 2019-06-12 11:11:27 +08:00
parent d2b87f2533
commit 6a64ec8ef9
8 changed files with 134 additions and 58 deletions

View File

@ -17,7 +17,7 @@ namespace php_arma
PHP_ARMA_METHOD(base, nElem, T, T1)
{
zend_long count;
count_elements(getThis(), &count);
count_elements(&EX(This), &count);
RETVAL_LONG(count);
}

View File

@ -31,7 +31,7 @@ namespace php_arma
auto has_imag = num_args == 2;
auto has_real = num_args > 0;
auto current = Z_OBJ_P(getThis());
auto current = Z_OBJ(EX(This));
if (EXPECTED(has_real)) {
if (!zval_check_scalar<T>(real)) {
@ -77,7 +77,7 @@ namespace php_arma
Z_PARAM_ZVAL(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::add(getThis(), other, return_value)) {
if (!operators::add(&EX(This), other, return_value)) {
ex_bad_type<T>(other);
}
}
@ -90,7 +90,7 @@ namespace php_arma
Z_PARAM_ZVAL(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::sub(getThis(), other, return_value)) {
if (!operators::sub(&EX(This), other, return_value)) {
ex_bad_type<T>(other);
}
}
@ -98,7 +98,7 @@ namespace php_arma
template <typename T>
PHP_ARMA_METHOD(complex, neg, T)
{
operators::neg(getThis(), return_value);
operators::neg(&EX(This), return_value);
}
template <typename T>
@ -109,7 +109,7 @@ namespace php_arma
Z_PARAM_ZVAL(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::mul(getThis(), other, return_value)) {
if (!operators::mul(&EX(This), other, return_value)) {
ex_bad_type<T>(other);
}
}
@ -122,7 +122,7 @@ namespace php_arma
Z_PARAM_ZVAL(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::div(getThis(), other, return_value)) {
if (!operators::div(&EX(This), other, return_value)) {
ex_bad_type<T>(other);
}
}
@ -130,48 +130,48 @@ namespace php_arma
template <typename T>
PHP_ARMA_METHOD(complex, abs, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::abs(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, arg, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::arg(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, norm, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::norm(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, conj, T)
{
operators::conj(getThis(), nullptr, return_value);
operators::conj(&EX(This), nullptr, return_value);
}
template <typename T>
PHP_ARMA_METHOD(complex, exp, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::exp(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, log, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::log(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, log10, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::log10(current));
}
@ -183,7 +183,7 @@ namespace php_arma
Z_PARAM_ZVAL(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::pow(getThis(), other, return_value)) {
if (!operators::pow(&EX(This), other, return_value)) {
ex_bad_type<T>(other);
}
}
@ -191,91 +191,91 @@ namespace php_arma
template <typename T>
PHP_ARMA_METHOD(complex, sqrt, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::abs(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, sin, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::sin(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, cos, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::cos(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, tan, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::tan(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, asin, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::asin(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, acos, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::acos(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, atan, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::atan(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, sinh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::sinh(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, cosh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::cosh(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, tanh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::tanh(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, asinh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::asinh(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, acosh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::acosh(current));
}
template <typename T>
PHP_ARMA_METHOD(complex, atanh, T)
{
auto current = zval_get_scalar<native_t>(getThis());
auto current = zval_get_scalar<native_t>(&EX(This));
zval_set_scalar(return_value, std::atanh(current));
}

View File

@ -27,21 +27,21 @@ namespace php_arma
static void invoke(T *obj, zend_long val)
{
switch (val) {
case fill::none:
case none:
break;
case fill::zeros:
case zeros:
obj->zeros();
break;
case fill::ones:
case ones:
obj->ones();
break;
case fill::eye:
case eye:
obj->eye();
break;
case fill::randu:
case randu:
obj->randu();
break;
case fill::randn:
case randn:
obj->randn();
break;
default:
@ -69,22 +69,22 @@ namespace php_arma
static arma::file_type native(zend_long val)
{
switch (val) {
case file_type::auto_detect:
case auto_detect:
return arma::file_type::auto_detect;
case file_type::arma_binary:
case arma_binary:
return arma::file_type::arma_binary;
case file_type::arma_ascii:
case arma_ascii:
return arma::file_type::arma_ascii;
case file_type::raw_binary:
case raw_binary:
return arma::file_type::raw_binary;
case file_type::csv_ascii:
case csv_ascii:
return arma::file_type::csv_ascii;
case file_type::coord_ascii:
case coord_ascii:
return arma::file_type::coord_ascii;
case file_type::pgm_binary:
case pgm_binary:
return arma::file_type::pgm_binary;
#ifdef ARMA_USE_HDF5
case file_type::hdf5_binary:
case hdf5_binary:
return arma::file_type::hdf5_binary;
#endif // ARMA_USE_HDF5
default:
@ -102,6 +102,23 @@ namespace php_arma
static constexpr auto descend = 1;
static constexpr auto strict_ascend = 2;
static constexpr auto strict_descend = 3;
zend_always_inline
static const char *native(zend_long val)
{
switch (val) {
case ascend:
return "ascend";
case descend:
return "decend";
case strict_ascend:
return "strictascend";
case strict_descend:
return "strictdescend";
default:
return nullptr;
}
}
};
constexpr const char sort_direction_php_name[] = "SortDirection";

View File

@ -18,7 +18,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::equals(getThis(), other, return_value);
operators::equals(&EX(This), other, return_value);
}
template <typename T, typename T1>
@ -29,7 +29,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::not_equals(getThis(), other, return_value);
operators::not_equals(&EX(This), other, return_value);
}
template <typename T, typename T1>
@ -40,7 +40,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::greater_than(getThis(), other, return_value);
operators::greater_than(&EX(This), other, return_value);
}
template <typename T, typename T1>
@ -51,7 +51,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::smaller_than(getThis(), other, return_value);
operators::smaller_than(&EX(This), other, return_value);
}
template <typename T, typename T1>
@ -62,7 +62,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::not_greater_than(getThis(), other, return_value);
operators::not_greater_than(&EX(This), other, return_value);
}
template <typename T, typename T1>
@ -73,7 +73,7 @@ namespace php_arma
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
operators::not_smaller_than(getThis(), other, return_value);
operators::not_smaller_than(&EX(This), other, return_value);
}
template <typename T, typename T1>

View File

@ -5,6 +5,7 @@
//
#include "dense_resizable_matrix.hh"
#include "constants.hh"
#include "dense_matrix.hh"
#include "resizable_matrix.hh"
#include "mat.hh"
@ -14,19 +15,49 @@ namespace php_arma
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_resizable_matrix, ones, T, T1)
{
zend_long n_rows = 0;
zend_long n_cols = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(n_rows)
Z_PARAM_LONG(n_cols)
ZEND_PARSE_PARAMETERS_END();
auto native = THIS_NATIVE;
native->ones(n_rows, n_cols);
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_resizable_matrix, randu, T, T1)
{
zend_long n_rows = 0;
zend_long n_cols = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(n_rows)
Z_PARAM_LONG(n_cols)
ZEND_PARSE_PARAMETERS_END();
auto native = THIS_NATIVE;
native->randu(n_rows, n_cols);
RETVAL_THIS();
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_resizable_matrix, randn, T, T1)
{
zend_long n_rows = 0;
zend_long n_cols = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(n_rows)
Z_PARAM_LONG(n_cols)
ZEND_PARSE_PARAMETERS_END();
auto native = THIS_NATIVE;
native->randn(n_rows, n_cols);
RETVAL_THIS();
}
template <typename T, typename T1>
@ -41,16 +72,37 @@ namespace php_arma
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_resizable_matrix, swap, T, T1)
template <typename T, typename ChildT>
PHP_ARMA_METHOD(dense_resizable_matrix, swap, T, ChildT)
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT(other)
ZEND_PARSE_PARAMETERS_END();
auto native = THIS_NATIVE;
if (UNEXPECTED(ChildT::ce != Z_OBJCE_P(other))) {
ex_bad_type(zval_get_type_name(&EX(This)), zval_get_type_name(other));
return;
}
native->swap(*Z_NATIVE_OBJ_P(other));
}
template <typename T, typename T1>
PHP_ARMA_METHOD(dense_resizable_matrix, isSorted, T, T1)
{
zend_long sort_direction = sort_direction::ascend;
zend_long dim = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_direction)
Z_PARAM_LONG(dim)
ZEND_PARSE_PARAMETERS_END();
auto native = THIS_NATIVE;
RETVAL_BOOL(native->is_sorted(sort_direction::native(sort_direction), dim));
}
template <typename T, typename T1>

View File

@ -14,7 +14,7 @@ namespace php_arma
template <typename T, bool B1, bool B2>
PHP_ARMA_METHOD(mapval, val, T, B1, B2)
{
auto current = Z_OBJ_P(getThis());
auto current = Z_OBJ(EX(This));
zval_set_scalar(return_value, get_val(current));
}
@ -30,7 +30,7 @@ namespace php_arma
return;
}
auto current = Z_OBJ_P(getThis());
auto current = Z_OBJ(EX(This));
set_val(current, zval_get_scalar<T>(val));
}

View File

@ -47,7 +47,7 @@
template class parent<zend_long, child<zend_long>>; \
template class parent<cx_double, child<cx_double>>
/// Helper macros for class entry and object handlers
/// Helper macros for class entry and object handlers.
#define PHP_ARMA_CE_HANDLRES_DECLARE() \
static inline zend_class_entry *ce; \
@ -112,7 +112,12 @@
to_native_object<native_t>(zobj)
#define Z_NATIVE_OBJ_P(zv) ZOBJ_NATIVE(Z_OBJ_P(zv))
#define THIS_NATIVE Z_NATIVE_OBJ_P(getThis())
#define THIS_NATIVE Z_NATIVE_OBJ_P(&EX(This))
/// Misc helper macros.
#define RETVAL_THIS() \
ZVAL_COPY(return_value, &EX(This))
namespace php_arma
{

View File

@ -65,6 +65,7 @@ namespace php_arma
auto native = THIS_NATIVE;
native->zeros(n_rows, n_cols);
RETVAL_THIS();
}
template <typename T, typename T1>
@ -80,6 +81,7 @@ namespace php_arma
auto native = THIS_NATIVE;
native->eye(n_rows, n_cols);
RETVAL_THIS();
}
template <typename T, typename T1>