--W/nzBZO5zC0uMSeA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
I do a lot of incrementing and decrementing of values: it'd be nice if
Numeric#succ had a counter part.
The attached patch provides Numeric#prev which is faster in tight
loops than (one less object):
i 0
p i.prev
-sc
PS Let me know if anyone has any interest in an inline
increment/decrement library.
i
i.incr
p i >> 2
i.decr
p i >> 1
--
Sean Chittenden
--W/nzBZO5zC0uMSeA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=patch
Index: numeric.c
RCS file: /src/ruby/numeric.c,v
retrieving revision 1.44
diff -u -r1.44 numeric.c
--- numeric.c 2002/04/24 05:08:04 1.44
+++ numeric.c 2002/04/24 05:20:55
@@ -1024,6 +1024,17 @@
}
static VALUE
+int_prev(num)
+ VALUE num;
+{
+ if (FIXNUM_P(num)) {
+ long i IX2LONG(num) - 1;
+ return rb_int2inum(i);
+ }
+ return rb_funcall(num, '-', 1, INT2FIX(1));
+}
+
+static VALUE
int_chr(num)
VALUE num;
{
@@ -1652,6 +1663,7 @@
rb_include_module(rb_cInteger, rb_mPrecision);
rb_define_method(rb_cInteger, "succ", int_succ, 0);
rb_define_method(rb_cInteger, "next", int_succ, 0);
+ rb_define_method(rb_cInteger, "prev", int_prev, 0);
rb_define_method(rb_cInteger, "chr", int_chr, 0);
rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
--W/nzBZO5zC0uMSeA--