split/Dvector/dvector.c in tioga-1.5 vs split/Dvector/dvector.c in tioga-1.6
- old
+ new
@@ -76,10 +76,21 @@
#define DVEC_DEFAULT_SIZE 16
PRIVATE bool Is_Dvector(VALUE obj) { return is_a_dvector(obj); }
+PRIVATE
+/* Checks if the given object is a Dvector. Mainly here for testing
+ purposes, as it corresponds to the internal +is_a_dvector+.
+*/
+VALUE dvector_is_a_dvector(VALUE self, VALUE obj)
+{
+ if(Is_Dvector(obj))
+ return Qtrue;
+ return Qfalse;
+}
+
static inline void dvector_mem_clear(double *mem, int size) {
while (size--) {
*mem++ = 0.0;
}
}
@@ -660,11 +671,12 @@
*
* a = Dvector[ 1, 2, 3, 4, 5 ]
* a.first -> 1
* a.first(1) -> Dvector[ 1 ]
* a.first(3) -> Dvector[ 1, 2, 3 ]
- */ VALUE dvector_first(int argc, VALUE *argv, VALUE ary) {
+ */
+VALUE dvector_first(int argc, VALUE *argv, VALUE ary) {
VALUE nv, result;
long n, i;
Dvector *d = Get_Dvector(ary);
if (argc == 0) {
if (d->len == 0) return Qnil;
@@ -1412,11 +1424,11 @@
if (d->len == 0) return rb_str_new(0, 0);
if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = Qtrue;
len += d->len * 10;
if (!NIL_P(sep)) {
StringValue(sep);
- len += RSTRING(sep)->len * (d->len - 1);
+ len += RSTRING_LEN(sep) * (d->len - 1); /* So it works for ruby 1.9 */
}
result = rb_str_buf_new(len);
for (i=0; i < d->len; i++) {
sprintf(buff, "%g", d->ptr[i]);
tmp = rb_str_new2(buff);
@@ -1435,11 +1447,12 @@
* Returns a string created by converting each element of the vector to
* a string, separated by <i>sep</i>.
*
* Dvector[ 1, 2, 3 ].join -> "1 2 3"
* Dvector[ 1, 2, 3 ].join("-") -> "1-2-3"
- */ VALUE dvector_join_m(int argc, VALUE *argv, VALUE ary) {
+ */
+VALUE dvector_join_m(int argc, VALUE *argv, VALUE ary) {
VALUE sep;
rb_scan_args(argc, argv, "01", &sep);
if (NIL_P(sep)) sep = dvector_output_fs;
return dvector_join(ary, sep);
}
@@ -4403,11 +4416,11 @@
}
}
if (!is_okay_number(v)) {
fclose(file);
- rb_raise(rb_eArgError, "ERROR: read found non-numeric value in line %i of %s -- %s", i, filename, num_str);
+ rb_raise(rb_eArgError, "ERROR: bad value %g in line %i of %s -- %s", v, i, filename, num_str);
}
if (row >= d->capa)
Dvector_Store_Double(col_obj, row, v);
else {
if (row > d->len) dvector_mem_clear(d->ptr + d->len, row - d->len + 1);
@@ -4514,11 +4527,11 @@
}
}
if (!is_okay_number(v)) {
fclose(file);
- rb_raise(rb_eArgError, "ERROR: non-finite value in file %s", filename);
+ rb_raise(rb_eArgError, "ERROR: bad value %g in line i% of file %s", v, i, filename);
}
if (col < d->capa) { row_data[col] = v; d->len = col+1; }
else {
Dvector_Store_Double(row_obj, col, v);
d = Get_Dvector(row_obj);
@@ -4607,11 +4620,11 @@
}
}
if (!is_okay_number(v)) {
fclose(file);
- rb_raise(rb_eArgError, "ERROR: non-finite value in file %s", filename);
+ rb_raise(rb_eArgError, "ERROR: bad value %g in line %i of file %s", v, i, filename);
}
Dvector_Store_Double(row_ary, col, v);
}
fclose(file);
return row_ary;
@@ -4989,24 +5002,26 @@
long target_len = 1 /* first signature byte */
+ 4 /* length */
+ len * 8 ;
unsigned u_len = (unsigned) len; /* this is bad, I know, but it
won't hurt before it is common
- that computers have 64 GB of RAM...
+ that computers have 32 GB of RAM...
*/
- VALUE str = rb_str_buf_new(target_len);
+
+ VALUE str = rb_str_new2("");
+ rb_str_resize(str,target_len); /* This seems to do the trick */
/* \begin{playing with ruby's internals} */
- unsigned char * ptr = (unsigned char *) RSTRING(str)->ptr;
+ unsigned char * ptr = (unsigned char *) RSTRING_PTR(str);
/* signature byte */
(*ptr++) = DVECTOR_DUMP_VERSION;
STORE_UNSIGNED(u_len, ptr); /* destroys u_len */
while(len-- > 0)
{
store_double(*(data++), ptr);
ptr += 8;
}
- RSTRING(str)->len = target_len;
+ /* RSTRING_LEN(str) = target_len; */
return str;
/* \end{playing with ruby's internals} */
}
PRIVATE
@@ -5017,11 +5032,11 @@
VALUE dvector_load(VALUE klass, VALUE str)
{
VALUE ret = Qnil;
VALUE s = StringValue(str);
unsigned char * buf = (unsigned char *) StringValuePtr(s);
- unsigned char * dest = buf + RSTRING(s)->len;
+ unsigned char * dest = buf + RSTRING_LEN(s);
unsigned i; /* for GET_UNSIGNED */
unsigned tmp = 0;
double * data;
/* depending on the first byte, the decoding will be different */
switch(*(buf++))
@@ -5215,9 +5230,12 @@
rb_define_singleton_method(cDvector, "create_spline_interpolant", dvector_create_spline_interpolant, -1);
rb_define_singleton_method(cDvector, "spline_interpolate", dvector_spline_interpolate, -1);
rb_define_singleton_method(cDvector, "linear_interpolate", dvector_linear_interpolate, -1);
rb_define_singleton_method(cDvector, "min_of_many", dvector_min_of_many, 1);
rb_define_singleton_method(cDvector, "max_of_many", dvector_max_of_many, 1);
+
+ rb_define_singleton_method(cDvector, "is_a_dvector", dvector_is_a_dvector, 1);
+
rb_define_method(cDvector, "make_bezier_control_points_for_cubic_in_x", dvector_make_bezier_control_points_for_cubic_in_x, 6);
rb_define_method(cDvector, "initialize", dvector_initialize, -1);
rb_define_method(cDvector, "initialize_copy", dvector_replace, 1);