Skip to content

Commit e315979

Browse files
committed
Changes to be compatible with c89 standard
1 parent 881866c commit e315979

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

clusterize.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,32 @@ zend_module_entry clusterize_module_entry = {
3939
STANDARD_MODULE_PROPERTIES
4040
};
4141

42-
ZEND_GET_MODULE(clusterize);
42+
ZEND_GET_MODULE(clusterize)
4343

4444
PHP_FUNCTION(clusterize)
4545
{
46-
zval *source_vectors;
46+
zval *source_vectors, *point, *lat, *lon, clusters, values;
4747
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;
4854

4955
if (zend_parse_parameters(2 TSRMLS_CC, "al", &source_vectors, &clusters_count) == FAILURE) {
5056
RETURN_FALSE;
5157
}
5258

53-
uint32_t n = Z_ARRVAL_P(source_vectors)->nNumOfElements;
59+
n = Z_ARRVAL_P(source_vectors)->nNumOfElements;
5460

5561
if (n == 0 || clusters_count == 0) {
5662
RETURN_FALSE;
5763
}
5864

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));
6466

67+
i = 0;
6568
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(source_vectors), point)
6669
{
6770
if (Z_TYPE_P(point) != IS_ARRAY)
@@ -76,10 +79,10 @@ PHP_FUNCTION(clusterize)
7679
RETURN_FALSE;
7780
}
7881

79-
HashTable *coordinates = HASH_OF(point);
82+
coordinates = HASH_OF(point);
8083

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);
8386

8487
if (lat == NULL || lon == NULL)
8588
{
@@ -98,9 +101,6 @@ PHP_FUNCTION(clusterize)
98101
++i;
99102
} ZEND_HASH_FOREACH_END();
100103

101-
kmeans_config config;
102-
kmeans_result result;
103-
104104
config.k = (uint32_t) clusters_count;
105105
config.num_objs = n;
106106
config.max_iterations = 200;
@@ -111,18 +111,18 @@ PHP_FUNCTION(clusterize)
111111
config.centers = ecalloc(config.k, sizeof(Pointer));
112112
config.clusters = ecalloc(config.num_objs, sizeof(int));
113113
config.clusters_sizes = ecalloc(config.k, sizeof(int));
114-
Point *init = ecalloc(config.k, sizeof(Point));
114+
init = ecalloc(config.k, sizeof(Point));
115115

116116
for (i = 0; i < config.num_objs; ++i)
117117
{
118118
config.objs[i] = &(points[i]);
119119
}
120120

121-
int part_size = lroundf(config.num_objs / config.k);
121+
part_size = lroundf(config.num_objs / config.k);
122122

123123
for (i = 0; i < config.k; ++i)
124124
{
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));
126126

127127
init[i] = points[r];
128128

@@ -145,8 +145,6 @@ PHP_FUNCTION(clusterize)
145145

146146
array_init_size(return_value, 2);
147147

148-
zval clusters;
149-
150148
array_init_size(&clusters, config.k);
151149

152150
for (i = 0; i < config.k; ++i)
@@ -165,8 +163,6 @@ PHP_FUNCTION(clusterize)
165163

166164
add_next_index_zval(return_value, &clusters);
167165

168-
zval values;
169-
170166
array_init_size(&values, config.num_objs);
171167

172168
for (i = 0; i < config.num_objs; ++i)

kmeans.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
static void update_r(kmeans_config *config)
2727
{
2828
int i;
29+
double distance, curr_distance;
30+
int cluster, curr_cluster;
31+
Pointer obj;
2932

3033
for (i = 0; i < config->num_objs; ++i)
3134
{
32-
double distance, curr_distance;
33-
int cluster, curr_cluster;
34-
Pointer obj;
35-
3635
obj = config->objs[i];
3736

3837
/*
@@ -80,7 +79,7 @@ static void update_means(kmeans_config *config)
8079

8180
kmeans_result kmeans(kmeans_config *config)
8281
{
83-
int iterations = 0;
82+
int iterations;
8483
int *clusters_last;
8584
size_t clusters_sz = sizeof(int) * config->num_objs;
8685
size_t clusters_size_sz = sizeof(int) * config->k;
@@ -105,6 +104,7 @@ kmeans_result kmeans(kmeans_config *config)
105104
*/
106105
clusters_last = kmeans_malloc(clusters_sz);
107106

107+
iterations = 0;
108108
while(1)
109109
{
110110
/* Store the previous state of the clustering */

points.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,35 @@
22

33
double pt_distance(const Pointer a, const Pointer b)
44
{
5-
Point *pa = (Point*) a;
6-
Point *pb = (Point*) b;
5+
Point *pa, *pb;
6+
double dx, dy;
77

8-
double dx = (pa->x - pb->x);
9-
double dy = (pa->y - pb->y);
8+
pa = (Point*) a;
9+
pb = (Point*) b;
10+
11+
dx = (pa->x - pb->x);
12+
dy = (pa->y - pb->y);
1013

1114
return dx * dx + dy * dy;
1215
}
1316

1417
void pt_centroid(const Pointer *objs, const int *clusters, size_t num_objs, int cluster, Pointer centroid)
1518
{
1619
int i;
17-
int num_cluster = 0;
20+
int num_cluster;
21+
Point sum, **pts, *center;
1822

19-
Point sum;
20-
Point **pts = (Point**) objs;
21-
Point *center = (Point*) centroid;
23+
pts = (Point**) objs;
24+
center = (Point*) centroid;
2225

2326
sum.x = sum.y = 0.0;
2427

2528
if (num_objs <= 0) {
2629
return;
2730
}
2831

32+
num_cluster = 0;
33+
2934
for (i = 0; i < num_objs; ++i)
3035
{
3136
if (clusters[i] != cluster) {
@@ -44,6 +49,4 @@ void pt_centroid(const Pointer *objs, const int *clusters, size_t num_objs, int
4449
sum.y /= num_cluster;
4550
*center = sum;
4651
}
47-
48-
return;
4952
}

0 commit comments

Comments
 (0)