summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-05 04:25:44 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-05 04:25:44 +0000
commit78be4478d176f9dc6f2e4fd8a5daa90173458568 (patch)
treeefee19ebee8620cf477872395f7c111dfd3b7f38
parent7841cb1ef1a786a9848742813cfde3591be511ae (diff)
downloadbundler-78be4478d176f9dc6f2e4fd8a5daa90173458568.tar.gz
implement Array-specific #all?, #none?, #one?
Before this patch Array#all? was not implemented in Array class and alternatively Enumerable#all? was used, while #any? has its own method entry in Array class. Similarly, Array#none? and #one? also lacks its own implementation. This patch provides Array-specific implementations for above three methods to enable faster method lookup. [Fix GH-2041] From: Koji Onishi <fursich0@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--array.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/array.c b/array.c
index cd22cb27cd..9ab67202db 100644
--- a/array.c
+++ b/array.c
@@ -6208,6 +6208,124 @@ rb_ary_any_p(int argc, VALUE *argv, VALUE ary)
}
/*
+ * call-seq:
+ * ary.all? [{|obj| block} ] -> true or false
+ * ary.all?(pattern) -> true or false
+ *
+ * See also Enumerable#all?
+ */
+
+static VALUE
+rb_ary_all_p(int argc, VALUE *argv, VALUE ary)
+{
+ long i, len = RARRAY_LEN(ary);
+
+ rb_check_arity(argc, 0, 1);
+ if (!len) return Qtrue;
+ if (argc) {
+ if (rb_block_given_p()) {
+ rb_warn("given block not used");
+ }
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (!RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) return Qfalse;
+ }
+ }
+ else if (!rb_block_given_p()) {
+ for (i = 0; i < len; ++i) {
+ if (!RTEST(RARRAY_AREF(ary, i))) return Qfalse;
+ }
+ }
+ else {
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (!RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qfalse;
+ }
+ }
+ return Qtrue;
+}
+
+/*
+ * call-seq:
+ * ary.none? [{|obj| block} ] -> true or false
+ * ary.none?(pattern) -> true or false
+ *
+ * See also Enumerable#none?
+ */
+
+static VALUE
+rb_ary_none_p(int argc, VALUE *argv, VALUE ary)
+{
+ long i, len = RARRAY_LEN(ary);
+
+ rb_check_arity(argc, 0, 1);
+ if (!len) return Qtrue;
+ if (argc) {
+ if (rb_block_given_p()) {
+ rb_warn("given block not used");
+ }
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) return Qfalse;
+ }
+ }
+ else if (!rb_block_given_p()) {
+ for (i = 0; i < len; ++i) {
+ if (RTEST(RARRAY_AREF(ary, i))) return Qfalse;
+ }
+ }
+ else {
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qfalse;
+ }
+ }
+ return Qtrue;
+}
+
+/*
+ * call-seq:
+ * ary.one? [{|obj| block} ] -> true or false
+ * ary.one?(pattern) -> true or false
+ *
+ * See also Enumerable#one?
+ */
+
+static VALUE
+rb_ary_one_p(int argc, VALUE *argv, VALUE ary)
+{
+ long i, len = RARRAY_LEN(ary);
+ VALUE result = Qfalse;
+
+ rb_check_arity(argc, 0, 1);
+ if (!len) return Qfalse;
+ if (argc) {
+ if (rb_block_given_p()) {
+ rb_warn("given block not used");
+ }
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) {
+ if (result) return Qfalse;
+ result = Qtrue;
+ }
+ }
+ }
+ else if (!rb_block_given_p()) {
+ for (i = 0; i < len; ++i) {
+ if (RTEST(RARRAY_AREF(ary, i))) {
+ if (result) return Qfalse;
+ result = Qtrue;
+ }
+ }
+ }
+ else {
+ for (i = 0; i < RARRAY_LEN(ary); ++i) {
+ if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) {
+ if (result) return Qfalse;
+ result = Qtrue;
+ }
+ }
+ }
+ return result;
+}
+
+/*
* call-seq:
* ary.dig(idx, ...) -> object
*
@@ -6765,6 +6883,9 @@ Init_Array(void)
rb_define_method(rb_cArray, "bsearch", rb_ary_bsearch, 0);
rb_define_method(rb_cArray, "bsearch_index", rb_ary_bsearch_index, 0);
rb_define_method(rb_cArray, "any?", rb_ary_any_p, -1);
+ rb_define_method(rb_cArray, "all?", rb_ary_all_p, -1);
+ rb_define_method(rb_cArray, "none?", rb_ary_none_p, -1);
+ rb_define_method(rb_cArray, "one?", rb_ary_one_p, -1);
rb_define_method(rb_cArray, "dig", rb_ary_dig, -1);
rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);