@@ -39,29 +39,32 @@ zend_module_entry clusterize_module_entry = {
39
39
STANDARD_MODULE_PROPERTIES
40
40
};
41
41
42
- ZEND_GET_MODULE (clusterize );
42
+ ZEND_GET_MODULE (clusterize )
43
43
44
44
PHP_FUNCTION (clusterize )
45
45
{
46
- zval * source_vectors ;
46
+ zval * source_vectors , * point , * lat , * lon , clusters , values ;
47
47
zend_long clusters_count ;
48
+ uint32_t i , n , r , part_size ;
49
+ Point * points , * init ;
50
+ kmeans_config config ;
51
+ kmeans_result result ;
52
+ HashTable * coordinates ;
53
+ HashPosition pos ;
48
54
49
55
if (zend_parse_parameters (2 TSRMLS_CC , "al" , & source_vectors , & clusters_count ) == FAILURE ) {
50
56
RETURN_FALSE ;
51
57
}
52
58
53
- uint32_t n = Z_ARRVAL_P (source_vectors )-> nNumOfElements ;
59
+ n = Z_ARRVAL_P (source_vectors )-> nNumOfElements ;
54
60
55
61
if (n == 0 || clusters_count == 0 ) {
56
62
RETURN_FALSE ;
57
63
}
58
64
59
- Point * points = (Point * ) ecalloc (n , sizeof (Point ));
60
-
61
- uint32_t i = 0 ;
62
- zval * point ;
63
- HashPosition pos ;
65
+ points = (Point * ) ecalloc (n , sizeof (Point ));
64
66
67
+ i = 0 ;
65
68
ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (source_vectors ), point )
66
69
{
67
70
if (Z_TYPE_P (point ) != IS_ARRAY )
@@ -76,10 +79,10 @@ PHP_FUNCTION(clusterize)
76
79
RETURN_FALSE ;
77
80
}
78
81
79
- HashTable * coordinates = HASH_OF (point );
82
+ coordinates = HASH_OF (point );
80
83
81
- zval * lat = zend_hash_index_find (coordinates , 0 );
82
- zval * lon = zend_hash_index_find (coordinates , 1 );
84
+ lat = zend_hash_index_find (coordinates , 0 );
85
+ lon = zend_hash_index_find (coordinates , 1 );
83
86
84
87
if (lat == NULL || lon == NULL )
85
88
{
@@ -98,9 +101,6 @@ PHP_FUNCTION(clusterize)
98
101
++ i ;
99
102
} ZEND_HASH_FOREACH_END ();
100
103
101
- kmeans_config config ;
102
- kmeans_result result ;
103
-
104
104
config .k = (uint32_t ) clusters_count ;
105
105
config .num_objs = n ;
106
106
config .max_iterations = 200 ;
@@ -111,18 +111,18 @@ PHP_FUNCTION(clusterize)
111
111
config .centers = ecalloc (config .k , sizeof (Pointer ));
112
112
config .clusters = ecalloc (config .num_objs , sizeof (int ));
113
113
config .clusters_sizes = ecalloc (config .k , sizeof (int ));
114
- Point * init = ecalloc (config .k , sizeof (Point ));
114
+ init = ecalloc (config .k , sizeof (Point ));
115
115
116
116
for (i = 0 ; i < config .num_objs ; ++ i )
117
117
{
118
118
config .objs [i ] = & (points [i ]);
119
119
}
120
120
121
- int part_size = lroundf (config .num_objs / config .k );
121
+ part_size = lroundf (config .num_objs / config .k );
122
122
123
123
for (i = 0 ; i < config .k ; ++ i )
124
124
{
125
- int r = i * part_size + lround (part_size * (1.0 * rand () / RAND_MAX ));
125
+ r = i * part_size + lround (part_size * (1.0 * rand () / RAND_MAX ));
126
126
127
127
init [i ] = points [r ];
128
128
@@ -145,8 +145,6 @@ PHP_FUNCTION(clusterize)
145
145
146
146
array_init_size (return_value , 2 );
147
147
148
- zval clusters ;
149
-
150
148
array_init_size (& clusters , config .k );
151
149
152
150
for (i = 0 ; i < config .k ; ++ i )
@@ -165,8 +163,6 @@ PHP_FUNCTION(clusterize)
165
163
166
164
add_next_index_zval (return_value , & clusters );
167
165
168
- zval values ;
169
-
170
166
array_init_size (& values , config .num_objs );
171
167
172
168
for (i = 0 ; i < config .num_objs ; ++ i )
0 commit comments