split/Dvector/dvector.c in tioga-1.8 vs split/Dvector/dvector.c in tioga-1.9

- old
+ new

@@ -1972,10 +1972,57 @@ } PRIVATE /* * call-seq: + * dvector.prune(lst) -> dvector + * + * Creates a new dvector without the entries given by the indexes in _lst_. + * + * a = Dvector.new(5) {|i| i*3 } -> [0, 3, 6, 9, 12] + * a.prune([0, 2]) -> [3, 9, 12] + * a -> [0, 3, 6, 9, 12] + */ +VALUE dvector_prune(VALUE ary, VALUE lst) { + ary = dvector_dup(ary); + dvector_prune_bang(ary, lst); + return ary; +} + +PRIVATE +/* + * call-seq: + * dvector.prune!(lst) -> dvector + * + * Modifies the dvector by removing the entries given by the indexes in _lst_. + * + * a = Dvector.new(5) {|i| i*3 } -> [0, 3, 6, 9, 12] + * a.prune!([0, 2]) -> [3, 9, 12] + * a -> [3, 9, 12] + */ +VALUE dvector_prune_bang(VALUE ary, VALUE lst) { + Dvector *d; + d = dvector_modify(ary); + int i, lst_len, ary_len, pos, j; + VALUE *lst_ptr; + lst = rb_Array(lst); + lst_ptr = RARRAY(lst)->ptr; + lst_len = RARRAY(lst)->len; + for (i = lst_len-1; i >= 0; i--) { + ary_len = d->len; + pos = NUM2INT(lst_ptr[i]); // remove this one from ary + for (j = pos+1; j < ary_len; j++, pos++) { + d->ptr[pos] = d->ptr[j]; + } + d->len = pos; + } + return ary; +} + +PRIVATE +/* + * call-seq: * dvector.slice!(int) -> number or nil * dvector.slice!(start, length) -> sub_vector or nil * dvector.slice!(range) -> sub_vector or nil * * Deletes the element(s) given by an index (optionally with a length) @@ -1997,11 +2044,12 @@ * a -> Dvector[] */ VALUE dvector_slice_bang(int argc, VALUE *argv, VALUE ary) { VALUE arg1, arg2; long pos, len; - Dvector *d = Get_Dvector(ary); + Dvector *d; + d = dvector_modify(ary); if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) { pos = NUM2LONG(arg1); len = NUM2LONG(arg2); delete_pos_len: if (pos < 0) { @@ -4531,11 +4579,11 @@ VALUE row_obj, rows_obj, *rows_ptr = NULL; Dvector *d; double v, *row_data; int buff_len = 1000; char *buff, *num_str, *pend, c, *cptr; - int num_rows = 0, i, row, col, buff_loc, c_loc, skip = first_row_of_file - 1; + int num_rows = 0, i, row, col, buff_loc, skip = first_row_of_file - 1; rows_obj = rb_Array(destinations); num_rows = RARRAY(rows_obj)->len; rows_ptr = RARRAY(rows_obj)->ptr; for (i = 0; i < num_rows; i++) { /* first pass to check validity */ row_obj = rows_ptr[i]; @@ -4738,10 +4786,31 @@ klass = Qnil; } /* C API functions */ +bool isa_Dvector(VALUE dvector) { + return Is_Dvector(dvector); + } + +long len_Dvector(VALUE dvector) { + Dvector *d = Get_Dvector(dvector); + return d->len; + } + +double access_Dvector(VALUE dvector, long offset) { + Dvector *d = Get_Dvector(dvector); + if (d->len == 0) return 0.0; + if (offset < 0) { + offset += d->len; + } + if (offset < 0 || d->len <= offset) { + return 0.0; + } + return d->ptr[offset]; + } + double *Dvector_Data_for_Read(VALUE dvector, long *len_ptr) { /* returns pointer to the dvector's data (which may be shared) */ Dvector *d = Get_Dvector(dvector); if(len_ptr) *len_ptr = d->len; return d->ptr; @@ -5520,10 +5589,12 @@ rb_define_method(cDvector, "reverse!", dvector_reverse_bang, 0); rb_define_method(cDvector, "sort", dvector_sort, 0); rb_define_method(cDvector, "sort!", dvector_sort_bang, 0); rb_define_method(cDvector, "collect", dvector_collect, 0); rb_define_method(cDvector, "collect!", dvector_collect_bang, 0); + rb_define_method(cDvector, "prune", dvector_prune, 1); + rb_define_method(cDvector, "prune!", dvector_prune_bang, 1); rb_define_alias(cDvector, "map", "collect"); rb_define_alias(cDvector, "map!", "collect!"); rb_define_method(cDvector, "dup", dvector_dup, 0); rb_define_method(cDvector, "select", dvector_select, 0); rb_define_method(cDvector, "values_at", dvector_values_at, -1); @@ -5638,10 +5709,11 @@ rb_define_method(cDvector, "tan", dvector_tan, 0); rb_define_method(cDvector, "tanh", dvector_tanh, 0); /* nonstandard math functions */ rb_define_method(cDvector, "neg", dvector_neg, 0); + rb_define_alias(cDvector, "-@", "neg"); rb_define_method(cDvector, "exp10", dvector_exp10, 0); rb_define_method(cDvector, "inv", dvector_inv, 0); rb_define_method(cDvector, "trim", dvector_trim, -1); rb_define_method(cDvector, "safe_log", dvector_safe_log, -1); rb_define_method(cDvector, "safe_log10", dvector_safe_log10, -1); @@ -5727,9 +5799,12 @@ /* end of modifications */ /* now, the fun part: exporting binary symbols. see include/dvector.h for their meaning */ + RB_EXPORT_SYMBOL(cDvector, isa_Dvector); + RB_EXPORT_SYMBOL(cDvector, len_Dvector); + RB_EXPORT_SYMBOL(cDvector, access_Dvector); RB_EXPORT_SYMBOL(cDvector, Dvector_Data_for_Read); RB_EXPORT_SYMBOL(cDvector, Dvector_Data_Copy); RB_EXPORT_SYMBOL(cDvector, Dvector_Data_for_Write); RB_EXPORT_SYMBOL(cDvector, Dvector_Data_for_Read); RB_EXPORT_SYMBOL(cDvector, Dvector_Data_Resize);