00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "ruby/ruby.h"
00013
00014 VALUE rb_mComparable;
00015
00016 static ID cmp;
00017
00018 void
00019 rb_cmperr(VALUE x, VALUE y)
00020 {
00021 const char *classname;
00022
00023 if (SPECIAL_CONST_P(y)) {
00024 y = rb_inspect(y);
00025 classname = StringValuePtr(y);
00026 }
00027 else {
00028 classname = rb_obj_classname(y);
00029 }
00030 rb_raise(rb_eArgError, "comparison of %s with %s failed",
00031 rb_obj_classname(x), classname);
00032 }
00033
00034 static VALUE
00035 cmp_eq_recursive(VALUE arg1, VALUE arg2, int recursive)
00036 {
00037 if (recursive) return Qfalse;
00038 return rb_funcall2(arg1, cmp, 1, &arg2);
00039 }
00040
00041 static VALUE
00042 cmp_eq(VALUE *a)
00043 {
00044 VALUE c = rb_exec_recursive_paired_outer(cmp_eq_recursive, a[0], a[1], a[1]);
00045
00046 if (NIL_P(c)) return Qfalse;
00047 if (rb_cmpint(c, a[0], a[1]) == 0) return Qtrue;
00048 return Qfalse;
00049 }
00050
00051 static VALUE
00052 cmp_failed(void)
00053 {
00054 return Qfalse;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 static VALUE
00067 cmp_equal(VALUE x, VALUE y)
00068 {
00069 VALUE a[2];
00070
00071 if (x == y) return Qtrue;
00072
00073 a[0] = x; a[1] = y;
00074 return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0);
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 static VALUE
00086 cmp_gt(VALUE x, VALUE y)
00087 {
00088 VALUE c = rb_funcall(x, cmp, 1, y);
00089
00090 if (rb_cmpint(c, x, y) > 0) return Qtrue;
00091 return Qfalse;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 static VALUE
00103 cmp_ge(VALUE x, VALUE y)
00104 {
00105 VALUE c = rb_funcall(x, cmp, 1, y);
00106
00107 if (rb_cmpint(c, x, y) >= 0) return Qtrue;
00108 return Qfalse;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 static VALUE
00120 cmp_lt(VALUE x, VALUE y)
00121 {
00122 VALUE c = rb_funcall(x, cmp, 1, y);
00123
00124 if (rb_cmpint(c, x, y) < 0) return Qtrue;
00125 return Qfalse;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 static VALUE
00137 cmp_le(VALUE x, VALUE y)
00138 {
00139 VALUE c = rb_funcall(x, cmp, 1, y);
00140
00141 if (rb_cmpint(c, x, y) <= 0) return Qtrue;
00142 return Qfalse;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 static VALUE
00161 cmp_between(VALUE x, VALUE min, VALUE max)
00162 {
00163 if (RTEST(cmp_lt(x, min))) return Qfalse;
00164 if (RTEST(cmp_gt(x, max))) return Qfalse;
00165 return Qtrue;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 void
00208 Init_Comparable(void)
00209 {
00210 #undef rb_intern
00211 #define rb_intern(str) rb_intern_const(str)
00212
00213 rb_mComparable = rb_define_module("Comparable");
00214 rb_define_method(rb_mComparable, "==", cmp_equal, 1);
00215 rb_define_method(rb_mComparable, ">", cmp_gt, 1);
00216 rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
00217 rb_define_method(rb_mComparable, "<", cmp_lt, 1);
00218 rb_define_method(rb_mComparable, "<=", cmp_le, 1);
00219 rb_define_method(rb_mComparable, "between?", cmp_between, 2);
00220
00221 cmp = rb_intern("<=>");
00222 }
00223