-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_clusterize.c
189 lines (143 loc) · 4.33 KB
/
php_clusterize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define HAVE_CLUSTERIZE 1
#include "php.h"
#include "php_clusterize.h"
#if HAVE_CLUSTERIZE
#define PHP_CLUSTERIZE_VERSION "1.0"
#define PHP_CLUSTERIZE_EXTNAME "clusterize"
extern zend_module_entry clusterize_module_entry;
#define phpext_clusterize_ptr &clusterize_module_entry
PHP_FUNCTION(clusterize);
static zend_function_entry clusterize_functions[] = {
PHP_FE(clusterize, NULL)
{NULL, NULL, NULL}
};
zend_module_entry clusterize_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_CLUSTERIZE_EXTNAME,
clusterize_functions,
NULL, // name of the MINIT function or NULL if not applicable
NULL, // name of the MSHUTDOWN function or NULL if not applicable
NULL, // name of the RINIT function or NULL if not applicable
NULL, // name of the RSHUTDOWN function or NULL if not applicable
NULL, // name of the MINFO function or NULL if not applicable
#if ZEND_MODULE_API_NO >= 20010901
PHP_CLUSTERIZE_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(clusterize)
PHP_FUNCTION(clusterize)
{
zval *source_vectors, *point, *lat, *lon, clusters, values;
zend_long clusters_count;
uint32_t i, n, r, part_size;
Point *points, *init;
kmeans_config config;
kmeans_result result;
HashTable *coordinates;
HashPosition pos;
if (zend_parse_parameters(2 TSRMLS_CC, "al", &source_vectors, &clusters_count) == FAILURE) {
RETURN_FALSE;
}
n = zend_hash_num_elements(Z_ARRVAL_P(source_vectors));
if (n == 0 || clusters_count == 0) {
RETURN_FALSE;
}
points = (Point *) ecalloc(n, sizeof(Point));
i = 0;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(source_vectors), point)
{
if (Z_TYPE_P(point) != IS_ARRAY)
{
efree(points);
RETURN_FALSE;
}
if (zend_hash_num_elements(Z_ARRVAL_P(point)) != 2)
{
efree(points);
RETURN_FALSE;
}
coordinates = HASH_OF(point);
lat = zend_hash_index_find(coordinates, 0);
lon = zend_hash_index_find(coordinates, 1);
if (lat == NULL || lon == NULL)
{
efree(points);
RETURN_FALSE;
}
convert_to_double(lat);
convert_to_double(lon);
points[i].x = Z_DVAL_P(lat);
points[i].y = Z_DVAL_P(lon);
zend_hash_move_forward_ex(Z_ARRVAL_P(source_vectors), &pos);
++i;
} ZEND_HASH_FOREACH_END();
config.k = (uint32_t) clusters_count;
config.num_objs = n;
config.max_iterations = 200;
config.distance_method = pt_distance;
config.centroid_method = pt_centroid;
config.objs = ecalloc(config.num_objs, sizeof(Pointer));
config.centers = ecalloc(config.k, sizeof(Pointer));
config.clusters = ecalloc(config.num_objs, sizeof(int));
config.clusters_sizes = ecalloc(config.k, sizeof(int));
init = ecalloc(config.k, sizeof(Point));
for (i = 0; i < config.num_objs; ++i)
{
config.objs[i] = &(points[i]);
}
part_size = (uint32_t) floor(config.num_objs / config.k);
for (i = 0; i < config.k; ++i)
{
#ifdef PHP_WIN32
r = (uint32_t) (i * part_size + floor(0.5 + part_size * (1.0 * rand() / RAND_MAX)));
#else
r = (uint32_t) (i * part_size + floor(part_size * (1.0 * rand() / RAND_MAX)));
#endif
init[i] = points[r];
config.centers[i] = &(init[i]);
}
result = kmeans(&config);
if (result == KMEANS_ERROR)
{
efree(points);
efree(init);
efree(config.objs);
efree(config.centers);
efree(config.clusters);
efree(config.clusters_sizes);
RETURN_FALSE;
}
array_init_size(return_value, 2);
array_init_size(&clusters, config.k);
for (i = 0; i < config.k; ++i)
{
zval *cluster_center = emalloc(3 * sizeof(zval *));
array_init_size(cluster_center, 3);
add_next_index_double(cluster_center, init[i].x);
add_next_index_double(cluster_center, init[i].y);
add_next_index_double(cluster_center, config.clusters_sizes[i]);
add_next_index_zval(&clusters, cluster_center);
efree(cluster_center);
}
add_next_index_zval(return_value, &clusters);
array_init_size(&values, config.num_objs);
for (i = 0; i < config.num_objs; ++i)
{
add_next_index_long(&values, config.clusters[i]);
}
add_next_index_zval(return_value, &values);
efree(points);
efree(init);
efree(config.objs);
efree(config.centers);
efree(config.clusters);
efree(config.clusters_sizes);
RETURN_ZVAL(return_value, 0, 0);
}
#endif