[前][次][番号順一覧][スレッド一覧]

ruby-changes:42721

From: akr <ko1@a...>
Date: Wed, 27 Apr 2016 19:59:27 +0900 (JST)
Subject: [ruby-changes:42721] akr:r54795 (trunk): {Fixnum, Bignum}#[] is unified into Integer.

akr	2016-04-27 20:56:03 +0900 (Wed, 27 Apr 2016)

  New Revision: 54795

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54795

  Log:
    {Fixnum,Bignum}#[] is unified into Integer.
    
    * numeric.c (int_aref): {Fixnum,Bignum}#[] is unified into
      Integer.
    
    * bignum.c (rb_big_aref): Don't define Bignum#<<.
    
    * internal.h (rb_big_aref): Declared.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54794)
+++ ChangeLog	(revision 54795)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Apr 27 20:53:59 2016  Tanaka Akira  <akr@f...>
+
+	* numeric.c (int_aref): {Fixnum,Bignum}#[] is unified into
+	  Integer.
+
+	* bignum.c (rb_big_aref): Don't define Bignum#<<.
+
+	* internal.h (rb_big_aref): Declared.
+
 Wed Apr 27 16:10:35 2016  NARUSE, Yui  <naruse@r...>
 
 	* tool/instruction.rb: fix to follow current implementation.
@@ -31,8 +40,6 @@ Tue Apr 26 21:11:02 2016  Tanaka Akira https://github.com/ruby/ruby/blob/trunk/ChangeLog#L40
 
 	* bignum.c (rb_big_lshift): Don't define Bignum#<<.
 
-	* internal.h (rb_big_lshift): Declared.
-
 Tue Apr 26 20:59:40 2016  Tanaka Akira  <akr@f...>
 
 	* numeric.c (rb_int_rshift): {Fixnum,Bignum}#>> is unified into
@@ -40,8 +47,6 @@ Tue Apr 26 20:59:40 2016  Tanaka Akira https://github.com/ruby/ruby/blob/trunk/ChangeLog#L47
 
 	* bignum.c (rb_big_rshift): Don't define Bignum#>>.
 
-	* internal.h (rb_big_rshift): Declared.
-
 Tue Apr 26 20:46:16 2016  Tanaka Akira  <akr@f...>
 
 	* numeric.c (int_size): {Fixnum,Bignum}#size is unified into Integer.
Index: internal.h
===================================================================
--- internal.h	(revision 54794)
+++ internal.h	(revision 54795)
@@ -781,6 +781,7 @@ size_t rb_big_size(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L781
 VALUE rb_integer_float_cmp(VALUE x, VALUE y);
 VALUE rb_integer_float_eq(VALUE x, VALUE y);
 VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
+VALUE rb_big_aref(VALUE x, VALUE y);
 VALUE rb_big_abs(VALUE x);
 VALUE rb_big_size_m(VALUE big);
 VALUE rb_big_bit_length(VALUE big);
Index: bignum.c
===================================================================
--- bignum.c	(revision 54794)
+++ bignum.c	(revision 54795)
@@ -6771,26 +6771,7 @@ rb_big_rshift(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6771
     }
 }
 
-/*
- *  call-seq:
- *     big[n] -> 0, 1
- *
- *  Bit Reference---Returns the <em>n</em>th bit in the (assumed) binary
- *  representation of <i>big</i>, where <i>big</i>[0] is the least
- *  significant bit.
- *
- *     a = 9**15
- *     50.downto(0) do |n|
- *       print a[n]
- *     end
- *
- *  <em>produces:</em>
- *
- *     000101110110100000111000011110010100111100010111001
- *
- */
-
-static VALUE
+VALUE
 rb_big_aref(VALUE x, VALUE y)
 {
     BDIGIT *xds;
@@ -7006,7 +6987,6 @@ Init_Bignum(void) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6987
     rb_define_method(rb_cBignum, "|", rb_big_or, 1);
     rb_define_method(rb_cBignum, "^", rb_big_xor, 1);
     rb_define_method(rb_cBignum, "~", rb_big_neg, 0);
-    rb_define_method(rb_cBignum, "[]", rb_big_aref, 1);
 
     rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
     rb_define_method(rb_cBignum, ">", big_gt, 1);
Index: numeric.c
===================================================================
--- numeric.c	(revision 54794)
+++ numeric.c	(revision 54795)
@@ -4025,20 +4025,6 @@ rb_int_rshift(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4025
     return Qnil;
 }
 
-/*
- *  call-seq:
- *     fix[n]  ->  0, 1
- *
- *  Bit Reference---Returns the +n+th bit in the binary representation of
- *  +fix+, where <code>fix[0]</code> is the least significant bit.
- *
- *  For example:
- *
- *     a = 0b11001100101010
- *     30.downto(0) do |n| print a[n] end
- *     #=> 0000000000000000011001100101010
- */
-
 static VALUE
 fix_aref(VALUE fix, VALUE idx)
 {
@@ -4068,6 +4054,38 @@ fix_aref(VALUE fix, VALUE idx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4054
 
 /*
  *  call-seq:
+ *     fix[n]  ->  0, 1
+ *
+ *  Bit Reference---Returns the +n+th bit in the binary representation of
+ *  +fix+, where <code>fix[0]</code> is the least significant bit.
+ *
+ *  For example:
+ *
+ *     a = 0b11001100101010
+ *     30.downto(0) do |n| print a[n] end
+ *     #=> 0000000000000000011001100101010
+ *
+ *     a = 9**15
+ *     50.downto(0) do |n|
+ *       print a[n]
+ *     end
+ *     #=> 000101110110100000111000011110010100111100010111001
+ */
+
+static VALUE
+int_aref(VALUE num, VALUE idx)
+{
+    if (FIXNUM_P(num)) {
+	return fix_aref(num, idx);
+    }
+    else if (RB_TYPE_P(num, T_BIGNUM)) {
+	return rb_big_aref(num, idx);
+    }
+    return Qnil;
+}
+
+/*
+ *  call-seq:
  *     int.to_f  ->  float
  *
  *  Converts +int+ to a +Float+.  If +int+ doesn't fit in a +Float+,
@@ -4703,7 +4721,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4721
     rb_define_method(rb_cFixnum, "&", fix_and, 1);
     rb_define_method(rb_cFixnum, "|", fix_or,  1);
     rb_define_method(rb_cFixnum, "^", fix_xor, 1);
-    rb_define_method(rb_cFixnum, "[]", fix_aref, 1);
+    rb_define_method(rb_cInteger, "[]", int_aref, 1);
 
     rb_define_method(rb_cInteger, "<<", rb_int_lshift, 1);
     rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]