Skip to content

Commit 2b8f500

Browse files
Implemented print_r static method for CArray printing.
1 parent 9abd01a commit 2b8f500

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ PHP_NEW_EXTENSION(phpsci,
1515
carray/initializers.c \
1616
carray/linalg.c \
1717
carray/ranges.c \
18+
kernel/carray_printer.c \
1819
carray/transformations.c,
1920
$ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
2021
PHP_INSTALL_HEADERS([ext/phpsci], [phpsci.h])

kernel/carray.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ CArray ptr_to_carray(MemoryPointer * ptr) {
8383
* Destroy target CArray and set last_deleted_uuid for posterior
8484
* allocation.
8585
*
86-
* @param uuid
87-
* @param rows
88-
* @param cols
86+
* @param uuid UUID of CArray to be destroyed
87+
* @param rows Number of rows in CArray to be destroyed
88+
* @param cols Number os cols in CArray to be destroyed
8989
*/
9090
void destroy_carray(int uuid, int rows, int cols) {
9191
free(PHPSCI_MAIN_MEM_STACK.buffer[uuid].array2d[0]);

kernel/carray_printer.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 7 | PHPSci |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2018 Henrique Borba |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Henrique Borba <henrique.borba.dev@gmail.com> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#include "carray_printer.h"
20+
#include "carray.h"
21+
#include "../phpsci.h"
22+
23+
/**
24+
* Print CArray 2D
25+
*
26+
* @author Henrique Borba <henrique.borba.dev@gmail.com>
27+
* @param target
28+
*/
29+
void print2d(CArray target, int x, int y) {
30+
int i, j;
31+
php_printf("[\n");
32+
for(i = 0; i < x; i++) {
33+
php_printf(" [");
34+
for(j = 0; j < y; j++) {
35+
php_printf(" %f ", target.array2d[i][j]);
36+
}
37+
php_printf("]\n");
38+
}
39+
php_printf("]");
40+
}
41+
42+
/**
43+
* Print CArray 1D
44+
*
45+
* @author Henrique Borba <henrique.borba.dev@gmail.com>
46+
* @param target
47+
*/
48+
void print1d(CArray target, int x) {
49+
int i;
50+
php_printf("[");
51+
for(i = 0; i < x; i ++) {
52+
php_printf(" %f ", target.array1d[i]);
53+
}
54+
php_printf("]");
55+
}
56+
57+
/**
58+
* Print CArray 0D
59+
*
60+
* @author Henrique Borba <henrique.borba.dev@gmail.com>
61+
* @param target
62+
*/
63+
void print0d(CArray target) {
64+
php_printf("%f", target.array0d);
65+
}
66+
67+
68+
/**
69+
* Print target CArray
70+
*
71+
* @author Henrique Borba <henrique.borba.dev@gmail.com>
72+
* @param x Number of rows (2D) or width (1D), if 0, is 0D
73+
* @param y Number of cols for 2D matrix
74+
*/
75+
void print_carray(MemoryPointer * ptr, int x, int y) {
76+
CArray target_carray = ptr_to_carray(ptr);
77+
if(target_carray.array0d != NULL && x == 0 && y == 0) {
78+
print0d(target_carray);
79+
return;
80+
}
81+
if(target_carray.array1d != NULL && x > 0 && y == 0) {
82+
print1d(target_carray, x);
83+
return;
84+
}
85+
if(target_carray.array2d != NULL && x > 0 && y > 0) {
86+
print2d(target_carray, x , y);
87+
return;
88+
}
89+
}
90+
91+

kernel/carray_printer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 7 | PHPSci |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2018 Henrique Borba |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Henrique Borba <henrique.borba.dev@gmail.com> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#ifndef PHPSCI_EXT_CARRAY_PRINTER_H
20+
#define PHPSCI_EXT_CARRAY_PRINTER_H
21+
22+
#include "carray.h"
23+
24+
void print_carray(MemoryPointer * ptr, int x, int y);
25+
void print0d(CArray target);
26+
void print1d(CArray target, int x);
27+
void print2d(CArray target, int x, int y);
28+
29+
30+
#endif //PHPSCI_EXT_CARRAY_PRINTER_H

phpsci.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "carray/linalg.h"
2626
#include "carray/transformations.h"
2727
#include "carray/ranges.h"
28+
#include "kernel/carray_printer.h"
2829
#include "kernel/memory_manager.h"
2930
#include "php.h"
3031

@@ -126,6 +127,17 @@ PHP_METHOD(CArray, transpose)
126127
object_init(return_value);
127128
set_obj_uuid(return_value, rtn.uuid);
128129
}
130+
PHP_METHOD(CArray, print_r) {
131+
long uuid, x, y;
132+
ZEND_PARSE_PARAMETERS_START(3, 3)
133+
Z_PARAM_LONG(uuid)
134+
Z_PARAM_LONG(x)
135+
Z_PARAM_LONG(y)
136+
ZEND_PARSE_PARAMETERS_END();
137+
MemoryPointer ptr;
138+
ptr.uuid = (int)uuid;
139+
print_carray(&ptr, x, y);
140+
}
129141
PHP_METHOD(CArray, toArray)
130142
{
131143
long uuid, rows, cols;
@@ -223,6 +235,8 @@ static zend_function_entry phpsci_class_methods[] =
223235
PHP_ME(CArray, toArray, NULL, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
224236
PHP_ME(CArray, fromArray, NULL, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
225237
PHP_ME(CArray, toDouble, NULL, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
238+
// VISUALIZATION
239+
PHP_ME(CArray, print_r, NULL, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
226240
{ NULL, NULL, NULL }
227241
};
228242

0 commit comments

Comments
 (0)