Skip to content

Commit 9286e70

Browse files
committed
Fix compatibility issues with Ruby 3.3.1
1 parent dd45491 commit 9286e70

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode/
2+
.idea
23
.yardoc/
34
.bundle/
45
log/*.log

ext/spatial_stats/csr_matrix.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ size_t csr_matrix_memsize(const void *ptr)
2222
return sizeof(*csr);
2323
}
2424

25+
const rb_data_type_t csr_matrix_type = {
26+
"SpatialStats::Weights::CSRMatrix",
27+
{NULL, csr_matrix_free, csr_matrix_memsize},
28+
0,
29+
0,
30+
RUBY_TYPED_FREE_IMMEDIATELY
31+
};
32+
2533
VALUE csr_matrix_alloc(VALUE self)
2634
{
27-
csr_matrix *csr = malloc(sizeof(csr_matrix));
35+
csr_matrix *csr = ALLOC(csr_matrix);
2836
return TypedData_Wrap_Struct(self, &csr_matrix_type, csr);
2937
}
3038

@@ -64,7 +72,7 @@ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows)
6472
// if it is, add array len to nnz
6573
row = rb_hash_aref(data, key);
6674
Check_Type(row, T_ARRAY);
67-
nnz += rb_array_len(row);
75+
nnz += (int)RARRAY_LEN(row); // Explicit cast to int
6876
}
6977

7078
values = malloc(sizeof(double) * nnz);
@@ -82,7 +90,7 @@ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows)
8290

8391
key = rb_ary_entry(keys, i);
8492
row = rb_hash_aref(data, key);
85-
m = rb_array_len(row);
93+
m = (int)RARRAY_LEN(row); // Explicit cast to int
8694

8795
for (j = 0; j < m; j++)
8896
{
@@ -140,7 +148,7 @@ VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows)
140148
keys = rb_funcall(data, rb_intern("keys"), 0);
141149

142150
// check dimensions are correct
143-
if (NUM2INT(num_rows) != rb_array_len(keys))
151+
if (NUM2INT(num_rows) != (int)RARRAY_LEN(keys)) // Explicit cast to int
144152
{
145153
rb_raise(rb_eArgError, "n_rows != keys.size, check your dimensions");
146154
}
@@ -249,7 +257,7 @@ VALUE csr_matrix_mulvec(VALUE self, VALUE vec)
249257

250258
TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
251259

252-
if (rb_array_len(vec) != csr->n)
260+
if (RARRAY_LEN(vec) != csr->n)
253261
{
254262
rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
255263
}
@@ -294,7 +302,7 @@ VALUE csr_matrix_dot_row(VALUE self, VALUE vec, VALUE row)
294302

295303
TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
296304

297-
if (rb_array_len(vec) != csr->n)
305+
if (RARRAY_LEN(vec) != csr->n)
298306
{
299307
rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
300308
}

ext/spatial_stats/csr_matrix.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ typedef struct csr_matrix
1414
void csr_matrix_free(void *mat);
1515
size_t csr_matrix_memsize(const void *ptr);
1616

17-
// ruby VALUE for csr_matrix
18-
static const rb_data_type_t csr_matrix_type = {
19-
"SpatialStats::Weights::CSRMatrix",
20-
{NULL, csr_matrix_free, csr_matrix_memsize},
21-
0,
22-
0,
23-
RUBY_TYPED_FREE_IMMEDIATELY};
17+
extern const rb_data_type_t csr_matrix_type;
2418

2519
void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows);
2620
VALUE csr_matrix_alloc(VALUE self);

ext/spatial_stats/spatial_stats.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void Init_spatial_stats()
1515
{
1616
VALUE spatial_stats_mod = rb_define_module("SpatialStats");
1717
VALUE weights_mod = rb_define_module_under(spatial_stats_mod, "Weights");
18-
VALUE csr_matrix_class = rb_define_class_under(weights_mod, "CSRMatrix", rb_cData);
18+
VALUE csr_matrix_class = rb_define_class_under(weights_mod, "CSRMatrix", rb_cObject);
1919

2020
rb_define_alloc_func(csr_matrix_class, csr_matrix_alloc);
2121
rb_define_method(csr_matrix_class, "initialize", csr_matrix_initialize, 2);

0 commit comments

Comments
 (0)