Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (90 sloc) 2.197 kb
817a62d 2000-05-01
matz authored
1 /**********************************************************************
99d0354 Initial revision
matz authored
2
3 compar.c -
4
5 $Author$
6 $Date$
7 created at: Thu Aug 26 14:39:48 JST 1993
8
42bc5d2 Updated Copyrights of Matz to 2003.
michal authored
9 Copyright (C) 1993-2003 Yukihiro Matsumoto
99d0354 Initial revision
matz authored
10
817a62d 2000-05-01
matz authored
11 **********************************************************************/
99d0354 Initial revision
matz authored
12
13 #include "ruby.h"
14
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
15 VALUE rb_mComparable;
99d0354 Initial revision
matz authored
16
17 static ID cmp;
18
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
19 int
20 rb_cmpint(val)
21 VALUE val;
22 {
23 if (FIXNUM_P(val)) return FIX2INT(val);
24 if (TYPE(val) == T_BIGNUM) {
25 if (RBIGNUM(val)->sign) return 1;
26 return -1;
27 }
28 if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
29 if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
30 return 0;
31 }
32
99d0354 Initial revision
matz authored
33 static VALUE
21d0b07 1.4.1
matz authored
34 cmp_equal(x, y)
35 VALUE x, y;
36 {
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
37 int c;
21d0b07 1.4.1
matz authored
38
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
39 if (x == y) return Qtrue;
40 c = rb_funcall(x, cmp, 1, y);
53f9f63 minor bug fixes.
matz authored
41 if (NIL_P(c)) return Qfalse;
4112162 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
42 if (c == INT2FIX(0)) return Qtrue;
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
43 if (rb_cmpint(c) == 0) return Qtrue;
f09bd39 * object.c (Init_Object): should do exact match for Module#==.
matz authored
44 return Qfalse;
21d0b07 1.4.1
matz authored
45 }
46
47 static VALUE
99d0354 Initial revision
matz authored
48 cmp_gt(x, y)
49 VALUE x, y;
50 {
51 VALUE c = rb_funcall(x, cmp, 1, y);
52
f09bd39 * object.c (Init_Object): should do exact match for Module#==.
matz authored
53 if (NIL_P(c)) return Qfalse;
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
54 if (rb_cmpint(c) > 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
55 return Qfalse;
99d0354 Initial revision
matz authored
56 }
57
58 static VALUE
59 cmp_ge(x, y)
60 VALUE x, y;
61 {
62 VALUE c = rb_funcall(x, cmp, 1, y);
63
f09bd39 * object.c (Init_Object): should do exact match for Module#==.
matz authored
64 if (NIL_P(c)) return Qfalse;
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
65 if (rb_cmpint(c) >= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
66 return Qfalse;
99d0354 Initial revision
matz authored
67 }
68
69 static VALUE
70 cmp_lt(x, y)
71 VALUE x, y;
72 {
73 VALUE c = rb_funcall(x, cmp, 1, y);
74
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
75 if (rb_cmpint(c) < 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
76 return Qfalse;
99d0354 Initial revision
matz authored
77 }
78
79 static VALUE
80 cmp_le(x, y)
81 VALUE x, y;
82 {
83 VALUE c = rb_funcall(x, cmp, 1, y);
84
f09bd39 * object.c (Init_Object): should do exact match for Module#==.
matz authored
85 if (NIL_P(c)) return Qfalse;
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
86 if (rb_cmpint(c) <= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
87 return Qfalse;
99d0354 Initial revision
matz authored
88 }
89
90 static VALUE
91 cmp_between(x, min, max)
92 VALUE x, min, max;
93 {
4112162 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
94 if (cmp_lt(x, min)) return Qfalse;
95 if (cmp_gt(x, max)) return Qfalse;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
96 return Qtrue;
99d0354 Initial revision
matz authored
97 }
98
99 void
100 Init_Comparable()
101 {
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
102 rb_mComparable = rb_define_module("Comparable");
21d0b07 1.4.1
matz authored
103 rb_define_method(rb_mComparable, "==", cmp_equal, 1);
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
104 rb_define_method(rb_mComparable, ">", cmp_gt, 1);
105 rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
106 rb_define_method(rb_mComparable, "<", cmp_lt, 1);
107 rb_define_method(rb_mComparable, "<=", cmp_le, 1);
108 rb_define_method(rb_mComparable, "between?", cmp_between, 2);
99d0354 Initial revision
matz authored
109
110 cmp = rb_intern("<=>");
111 }
Something went wrong with that request. Please try again.