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

ruby-changes:49032

From: naruse <ko1@a...>
Date: Tue, 12 Dec 2017 18:12:18 +0900 (JST)
Subject: [ruby-changes:49032] naruse:r61147 (trunk): Integer#allbits?, Integer#anybits?, Integer#nobits? [Feature #12753]

naruse	2017-12-12 18:12:14 +0900 (Tue, 12 Dec 2017)

  New Revision: 61147

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

  Log:
    Integer#allbits?, Integer#anybits?, Integer#nobits? [Feature #12753]

  Modified files:
    trunk/NEWS
    trunk/numeric.c
    trunk/test/ruby/test_integer_comb.rb
Index: NEWS
===================================================================
--- NEWS	(revision 61146)
+++ NEWS	(revision 61147)
@@ -82,6 +82,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L82
     [Bug #13420]
   * Integer#pow now has an optional modulo argument for calculating modular
     exponentiation.  [Feature #12508] [Feature #11003]
+  * Integer#allbits?, Integer#anybits?, Integer#nobits? [Feature #12753]
 
 * Kernel
 
Index: test/ruby/test_integer_comb.rb
===================================================================
--- test/ruby/test_integer_comb.rb	(revision 61146)
+++ test/ruby/test_integer_comb.rb	(revision 61147)
@@ -457,6 +457,30 @@ class TestIntegerComb < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer_comb.rb#L457
     }
   end
 
+  def test_allbits_p
+    VS.each {|a|
+      VS.each {|b|
+        assert_equal((a & b) == b, a.allbits?(b), "(#{a}).allbits?(#{b}")
+      }
+    }
+  end
+
+  def test_anybits_p
+    VS.each {|a|
+      VS.each {|b|
+        assert_equal((a & b) != 0, a.anybits?(b), "(#{a}).anybits?(#{b}")
+      }
+    }
+  end
+
+  def test_nobits_p
+    VS.each {|a|
+      VS.each {|b|
+        assert_equal((a & b) == 0, a.nobits?(b), "(#{a}).nobits?(#{b}")
+      }
+    }
+  end
+
   def test_to_s
     2.upto(36) {|radix|
       VS.each {|a|
Index: numeric.c
===================================================================
--- numeric.c	(revision 61146)
+++ numeric.c	(revision 61147)
@@ -3166,6 +3166,45 @@ int_even_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3166
 }
 
 /*
+ *  call-seq:
+ *     int.allbits?(mask)  ->  true or false
+ *
+ *  Returns +true+ if all bits of <code>+int+ & +mask+</code> is 1.
+ */
+
+static VALUE
+int_allbits_p(VALUE num, VALUE mask)
+{
+    return rb_int_equal(rb_int_and(num, mask), mask);
+}
+
+/*
+ *  call-seq:
+ *     int.anybits?(mask)  ->  true or false
+ *
+ *  Returns +true+ if any bits of <code>+int+ & +mask+</code> is 1.
+ */
+
+static VALUE
+int_anybits_p(VALUE num, VALUE mask)
+{
+    return num_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue;
+}
+
+/*
+ *  call-seq:
+ *     int.nobits?(mask)  ->  true or false
+ *
+ *  Returns +true+ if no bits of <code>+int+ & +mask+</code> is 1.
+ */
+
+static VALUE
+int_nobits_p(VALUE num, VALUE mask)
+{
+    return num_zero_p(rb_int_and(num, mask));
+}
+
+/*
  *  Document-method: Integer#succ
  *  Document-method: Integer#next
  *  call-seq:
@@ -5352,6 +5391,9 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5391
     rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
     rb_define_method(rb_cInteger, "odd?", rb_int_odd_p, 0);
     rb_define_method(rb_cInteger, "even?", int_even_p, 0);
+    rb_define_method(rb_cInteger, "allbits?", int_allbits_p, 1);
+    rb_define_method(rb_cInteger, "anybits?", int_anybits_p, 1);
+    rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
     rb_define_method(rb_cInteger, "upto", int_upto, 1);
     rb_define_method(rb_cInteger, "downto", int_downto, 1);
     rb_define_method(rb_cInteger, "times", int_dotimes, 0);

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

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