int ll_sort(t_ll_list *list, int (*f_cmp)(void *, void *)) { t_ll_node *w; t_ll_node *wp; if (!list) return (-1); else if (list->count < 2) return (0); w = list->head; while (w->next) { if (f_cmp(w->data, w->next->data) > 0) { wp = w->next; while (wp->previous && f_cmp(wp->data, wp->previous->data) < 0) { static_ll_swap(wp, wp->previous); wp = wp->previous; } } w = w->next; } return (0); }
inline static VALUE nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den) { VALUE gcd; switch (FIX2INT(f_cmp(den, ZERO))) { case -1: num = f_negate(num); den = f_negate(den); break; case 0: rb_raise_zerodiv(); break; } gcd = f_gcd(num, den); num = f_idiv(num, gcd); den = f_idiv(den, gcd); #ifdef CANON if (f_one_p(den) && canonicalization) return num; #endif return nurat_s_new_internal(klass, num, den); }
static VALUE nurat_s_new_bang(int argc, VALUE *argv, VALUE klass) { VALUE num, den; switch (rb_scan_args(argc, argv, "11", &num, &den)) { case 1: if (!k_integer_p(num)) num = f_to_i(num); den = ONE; break; default: if (!k_integer_p(num)) num = f_to_i(num); if (!k_integer_p(den)) den = f_to_i(den); switch (FIX2INT(f_cmp(den, ZERO))) { case -1: num = f_negate(num); den = f_negate(den); break; case 0: rb_raise_zerodiv(); break; } break; } return nurat_s_new_internal(klass, num, den); }
static VALUE nurat_cmp(VALUE self, VALUE other) { switch (TYPE(other)) { case T_FIXNUM: case T_BIGNUM: { get_dat1(self); if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1) return f_cmp(dat->num, other); else return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other)); } case T_FLOAT: return f_cmp(f_to_f(self), other); case T_RATIONAL: { VALUE num1, num2; get_dat2(self, other); if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) && FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) { num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den)); num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den)); } else { num1 = f_mul(adat->num, bdat->den); num2 = f_mul(bdat->num, adat->den); } return f_cmp(f_sub(num1, num2), ZERO); } default: return rb_num_coerce_bin(self, other, id_cmp); } }
/* * call-seq: * rat ** numeric -> numeric_result * * Performs exponentiation. * * For example: * * Rational(2) ** Rational(3) #=> (8/1) * Rational(10) ** -2 #=> (1/100) * Rational(10) ** -2.0 #=> 0.01 * Rational(-4) ** Rational(1,2) #=> (1.2246063538223773e-16+2.0i) * Rational(1, 2) ** 0 #=> (1/1) * Rational(1, 2) ** 0.0 #=> 1.0 */ static VALUE nurat_expt(VALUE self, SEL sel, VALUE other) { if (k_exact_zero_p(other)) return f_rational_new_bang1(CLASS_OF(self), ONE); if (k_rational_p(other)) { get_dat1(other); if (f_one_p(dat->den)) other = dat->num; /* c14n */ } switch (TYPE(other)) { case T_FIXNUM: { VALUE num, den; get_dat1(self); switch (FIX2INT(f_cmp(other, ZERO))) { case 1: num = f_expt(dat->num, other); den = f_expt(dat->den, other); break; case -1: num = f_expt(dat->den, f_negate(other)); den = f_expt(dat->num, f_negate(other)); break; default: num = ONE; den = ONE; break; } return f_rational_new2(CLASS_OF(self), num, den); } case T_BIGNUM: rb_warn("in a**b, b may be too big"); /* fall through */ case T_FLOAT: case T_RATIONAL: return f_expt(f_to_f(self), other); default: return rb_num_coerce_bin(self, other, id_expt); } }
inline static VALUE nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den) { switch (FIX2INT(f_cmp(den, ZERO))) { case -1: num = f_negate(num); den = f_negate(den); break; case 0: rb_raise(rb_eZeroDivError, "devided by zero"); break; } if (f_equal_p(den, ONE) && f_unify_p(klass)) return num; else return nurat_s_new_internal(klass, num, den); }
/** * Finds file(s) with current SNP(s) with lowest coordinates. * Sets values in is_lowest and lowest arrays: * - is_lowest is of length n_vcf and has TRUE or FALSE flags. * - lowest has *n_lowest values which are indices pointing * to elements in f_info array. */ void find_lowest(FileInfo *f_info, int n_vcf, int *is_lowest, int *lowest, int *n_lowest) { int i, j; FileInfo *cur_lowest; *n_lowest = 0; for(i = 0; i < n_vcf; i++) { is_lowest[i] = FALSE; } for(i = 0; i < n_vcf; i++) { if(f_info[i].is_done) { /* at end of this file */ continue; } if(*n_lowest == 0) { /* first file that is not at end */ lowest[0] = i; *n_lowest = 1; cur_lowest = &f_info[i]; } else { /* is this file's SNP lower than current lowest? */ int c = f_cmp(&f_info[i], cur_lowest); if(c < 0) { /* new lowest SNP */ cur_lowest = &f_info[i]; lowest[0] = i; *n_lowest = 1; } else if(c == 0) { /* another SNP that matches lowest */ lowest[*n_lowest] = i; *n_lowest += 1; } } } for(i = 0; i < *n_lowest; i++) { is_lowest[lowest[i]] = TRUE; } }
inline static VALUE nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den) { VALUE gcd; switch (FIX2INT(f_cmp(den, ZERO))) { case -1: num = f_negate(num); den = f_negate(den); break; case 0: rb_raise(rb_eZeroDivError, "devided by zero"); break; } gcd = f_gcd(num, den); num = f_idiv(num, gcd); den = f_idiv(den, gcd); if (f_one_p(den) && f_unify_p(klass)) return num; else return nurat_s_new_internal(klass, num, den); }