compar.c


DEFINITIONS

This source file includes following functions.
  1. cmp_equal
  2. cmp_gt
  3. cmp_ge
  4. cmp_lt
  5. cmp_le
  6. cmp_between
  7. Init_Comparable


   1  /**********************************************************************
   2  
   3    compar.c -
   4  
   5    $Author: matz $
   6    $Date: 2002/09/03 05:20:06 $
   7    created at: Thu Aug 26 14:39:48 JST 1993
   8  
   9    Copyright (C) 1993-2002 Yukihiro Matsumoto
  10  
  11  **********************************************************************/
  12  
  13  #include "ruby.h"
  14  
  15  VALUE rb_mComparable;
  16  
  17  static ID cmp;
  18  
  19  static VALUE
  20  cmp_equal(x, y)
  21      VALUE x, y;
  22  {
  23      VALUE c = rb_funcall(x, cmp, 1, y);
  24  
  25      if (NIL_P(c)) return Qfalse;
  26      if (c == INT2FIX(0)) return Qtrue;
  27      if (TYPE(c) == T_BIGNUM) {
  28          if (rb_big_norm(c) == INT2FIX(0)) return Qtrue;
  29      }
  30      return Qfalse;
  31  }
  32  
  33  static VALUE
  34  cmp_gt(x, y)
  35      VALUE x, y;
  36  {
  37      VALUE c = rb_funcall(x, cmp, 1, y);
  38  
  39      if (NIL_P(c)) return Qfalse;
  40      if (FIXNUM_P(c) && FIX2INT(c) > 0) return Qtrue;
  41      if (TYPE(c) == T_BIGNUM) {
  42          if (rb_big_norm(x) == INT2FIX(0)) return Qfalse;
  43          if (RBIGNUM(c)->sign) return Qtrue;
  44      }
  45      return Qfalse;
  46  }
  47  
  48  static VALUE
  49  cmp_ge(x, y)
  50      VALUE x, y;
  51  {
  52      VALUE c = rb_funcall(x, cmp, 1, y);
  53  
  54      if (NIL_P(c)) return Qfalse;
  55      if (FIXNUM_P(c) && FIX2INT(c) >= 0) return Qtrue;
  56      if (TYPE(c) == T_BIGNUM) {
  57          if (rb_big_norm(x) == INT2FIX(0)) return Qtrue;
  58          if (RBIGNUM(c)->sign) return Qtrue;
  59      }
  60      return Qfalse;
  61  }
  62  
  63  static VALUE
  64  cmp_lt(x, y)
  65      VALUE x, y;
  66  {
  67      VALUE c = rb_funcall(x, cmp, 1, y);
  68  
  69      if (FIXNUM_P(c) && FIX2INT(c) < 0) return Qtrue;
  70      if (TYPE(c) == T_BIGNUM) {
  71          if (rb_big_norm(x) == INT2FIX(0)) return Qfalse;
  72          if (!RBIGNUM(c)->sign) return Qtrue;
  73      }
  74      return Qfalse;
  75  }
  76  
  77  static VALUE
  78  cmp_le(x, y)
  79      VALUE x, y;
  80  {
  81      VALUE c = rb_funcall(x, cmp, 1, y);
  82  
  83      if (NIL_P(c)) return Qfalse;
  84      if (FIXNUM_P(c) && FIX2INT(c) <= 0) return Qtrue;
  85      if (TYPE(c) == T_BIGNUM) {
  86          if (rb_big_norm(x) == INT2FIX(0)) return Qtrue;
  87          if (!RBIGNUM(c)->sign) return Qtrue;
  88      }
  89      return Qfalse;
  90  }
  91  
  92  static VALUE
  93  cmp_between(x, min, max)
  94      VALUE x, min, max;
  95  {
  96      if (cmp_lt(x, min)) return Qfalse;
  97      if (cmp_gt(x, max)) return Qfalse;
  98      return Qtrue;
  99  }
 100  
 101  void
 102  Init_Comparable()
 103  {
 104      rb_mComparable = rb_define_module("Comparable");
 105      rb_define_method(rb_mComparable, "==", cmp_equal, 1);
 106      rb_define_method(rb_mComparable, ">", cmp_gt, 1);
 107      rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
 108      rb_define_method(rb_mComparable, "<", cmp_lt, 1);
 109      rb_define_method(rb_mComparable, "<=", cmp_le, 1);
 110      rb_define_method(rb_mComparable, "between?", cmp_between, 2);
 111  
 112      cmp = rb_intern("<=>");
 113  }