diff --git a/src/base.cc b/src/base.cc index 04ba8c7..b10b960 100644 --- a/src/base.cc +++ b/src/base.cc @@ -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); } diff --git a/src/complex.cc b/src/complex.cc index 9bdd70e..522c9cc 100644 --- a/src/complex.cc +++ b/src/complex.cc @@ -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(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(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(other); } } @@ -98,7 +98,7 @@ namespace php_arma template PHP_ARMA_METHOD(complex, neg, T) { - operators::neg(getThis(), return_value); + operators::neg(&EX(This), return_value); } template @@ -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(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(other); } } @@ -130,48 +130,48 @@ namespace php_arma template PHP_ARMA_METHOD(complex, abs, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::abs(current)); } template PHP_ARMA_METHOD(complex, arg, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::arg(current)); } template PHP_ARMA_METHOD(complex, norm, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::norm(current)); } template PHP_ARMA_METHOD(complex, conj, T) { - operators::conj(getThis(), nullptr, return_value); + operators::conj(&EX(This), nullptr, return_value); } template PHP_ARMA_METHOD(complex, exp, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::exp(current)); } template PHP_ARMA_METHOD(complex, log, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::log(current)); } template PHP_ARMA_METHOD(complex, log10, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&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(other); } } @@ -191,91 +191,91 @@ namespace php_arma template PHP_ARMA_METHOD(complex, sqrt, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::abs(current)); } template PHP_ARMA_METHOD(complex, sin, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::sin(current)); } template PHP_ARMA_METHOD(complex, cos, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::cos(current)); } template PHP_ARMA_METHOD(complex, tan, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::tan(current)); } template PHP_ARMA_METHOD(complex, asin, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::asin(current)); } template PHP_ARMA_METHOD(complex, acos, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::acos(current)); } template PHP_ARMA_METHOD(complex, atan, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::atan(current)); } template PHP_ARMA_METHOD(complex, sinh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::sinh(current)); } template PHP_ARMA_METHOD(complex, cosh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::cosh(current)); } template PHP_ARMA_METHOD(complex, tanh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::tanh(current)); } template PHP_ARMA_METHOD(complex, asinh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::asinh(current)); } template PHP_ARMA_METHOD(complex, acosh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::acosh(current)); } template PHP_ARMA_METHOD(complex, atanh, T) { - auto current = zval_get_scalar(getThis()); + auto current = zval_get_scalar(&EX(This)); zval_set_scalar(return_value, std::atanh(current)); } diff --git a/src/constants.hh b/src/constants.hh index b061bae..f1c0d71 100644 --- a/src/constants.hh +++ b/src/constants.hh @@ -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"; diff --git a/src/dense.cc b/src/dense.cc index 9a94bd2..0ad6fe4 100644 --- a/src/dense.cc +++ b/src/dense.cc @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/dense_resizable_matrix.cc b/src/dense_resizable_matrix.cc index 2772f5e..ea4e262 100644 --- a/src/dense_resizable_matrix.cc +++ b/src/dense_resizable_matrix.cc @@ -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 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 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 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 @@ -41,16 +72,37 @@ namespace php_arma } - template - PHP_ARMA_METHOD(dense_resizable_matrix, swap, T, T1) + template + 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 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 diff --git a/src/mapval.cc b/src/mapval.cc index 4765c2c..1a81df6 100644 --- a/src/mapval.cc +++ b/src/mapval.cc @@ -14,7 +14,7 @@ namespace php_arma template 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(val)); } diff --git a/src/php_arma.hh b/src/php_arma.hh index 05d4891..238b3ab 100644 --- a/src/php_arma.hh +++ b/src/php_arma.hh @@ -47,7 +47,7 @@ template class parent>; \ template class parent> -/// 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(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 { diff --git a/src/resizable_matrix.cc b/src/resizable_matrix.cc index f326b26..62f9793 100644 --- a/src/resizable_matrix.cc +++ b/src/resizable_matrix.cc @@ -65,6 +65,7 @@ namespace php_arma auto native = THIS_NATIVE; native->zeros(n_rows, n_cols); + RETVAL_THIS(); } template @@ -80,6 +81,7 @@ namespace php_arma auto native = THIS_NATIVE; native->eye(n_rows, n_cols); + RETVAL_THIS(); } template