summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-02 07:50:18 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-02 07:50:18 (GMT)
commit7495b2d2d65c34c8404feb0dade32efb3ac1f623 (patch)
treef99ae6211b3f30e9f4a6590531b7120c19c00d0e /enum.c
parent4973f30474126ee49b9a622b0abae0f24cac88a6 (diff)
enum.c: bignum counter
* enum.c (imemo_count_up, imemo_count_value): promote the counter value to a bignum on overflow. [Bug #14805] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63554 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c50
1 files changed, 40 insertions, 10 deletions
diff --git a/enum.c b/enum.c
index 967bc6b..e57fa5e 100644
--- a/enum.c
+++ b/enum.c
@@ -142,6 +142,34 @@ enum_grep_v(VALUE obj, VALUE pat)
return ary;
}
+#define COUNT_BIGNUM IMEMO_FL_USER0
+#define MEMO_V3_SET(m, v) RB_OBJ_WRITE((m), &(m)->u3.value, (v))
+
+static void
+imemo_count_up(struct MEMO *memo)
+{
+ if (memo->flags & COUNT_BIGNUM) {
+ MEMO_V3_SET(memo, rb_int_succ(memo->u3.value));
+ }
+ else if (++memo->u3.cnt == 0) {
+ /* overflow */
+ unsigned long buf[2] = {0, 1};
+ MEMO_V3_SET(memo, rb_big_unpack(buf, 2));
+ memo->flags |= COUNT_BIGNUM;
+ }
+}
+
+static VALUE
+imemo_count_value(struct MEMO *memo)
+{
+ if (memo->flags & COUNT_BIGNUM) {
+ return memo->u3.value;
+ }
+ else {
+ return ULONG2NUM(memo->u3.cnt);
+ }
+}
+
static VALUE
count_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
{
@@ -150,7 +178,7 @@ count_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
ENUM_WANT_SVALUE();
if (rb_equal(i, memo->v1)) {
- memo->u3.cnt++;
+ imemo_count_up(memo);
}
return Qnil;
}
@@ -161,7 +189,7 @@ count_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
struct MEMO *memo = MEMO_CAST(memop);
if (RTEST(rb_yield_values2(argc, argv))) {
- memo->u3.cnt++;
+ imemo_count_up(memo);
}
return Qnil;
}
@@ -171,7 +199,7 @@ count_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
{
struct MEMO *memo = MEMO_CAST(memop);
- memo->u3.cnt++;
+ imemo_count_up(memo);
return Qnil;
}
@@ -218,7 +246,7 @@ enum_count(int argc, VALUE *argv, VALUE obj)
memo = MEMO_NEW(item, 0, 0);
rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
- return ULONG2NUM(memo->u3.cnt);
+ return imemo_count_value(memo);
}
static VALUE
@@ -286,10 +314,10 @@ find_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
ENUM_WANT_SVALUE();
if (rb_equal(i, memo->v2)) {
- MEMO_V1_SET(memo, ULONG2NUM(memo->u3.cnt));
+ MEMO_V1_SET(memo, imemo_count_value(memo));
rb_iter_break();
}
- memo->u3.cnt++;
+ imemo_count_up(memo);
return Qnil;
}
@@ -299,10 +327,10 @@ find_index_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
struct MEMO *memo = MEMO_CAST(memop);
if (RTEST(rb_yield_values2(argc, argv))) {
- MEMO_V1_SET(memo, ULONG2NUM(memo->u3.cnt));
+ MEMO_V1_SET(memo, imemo_count_value(memo));
rb_iter_break();
}
- memo->u3.cnt++;
+ imemo_count_up(memo);
return Qnil;
}
@@ -2232,9 +2260,11 @@ enum_member(VALUE obj, VALUE val)
static VALUE
each_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo))
{
- long n = MEMO_CAST(memo)->u3.cnt++;
+ struct MEMO *m = MEMO_CAST(memo);
+ VALUE n = imemo_count_value(m);
- return rb_yield_values(2, rb_enum_values_pack(argc, argv), ULONG2NUM(n));
+ imemo_count_up(m);
+ return rb_yield_values(2, rb_enum_values_pack(argc, argv), n);
}
/*