Moved Array#shuffle and Array#shuffle! to rbinc

This commit is contained in:
Nobuyoshi Nakada 2020-01-26 18:27:40 +09:00
Родитель 0aa5195262
Коммит 29eb1b1602
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4BC7D6DF58D8DF60
5 изменённых файлов: 53 добавлений и 54 удалений

56
array.c
Просмотреть файл

@ -29,6 +29,7 @@
#include "ruby/st.h" #include "ruby/st.h"
#include "ruby/util.h" #include "ruby/util.h"
#include "transient_heap.h" #include "transient_heap.h"
#include "builtin.h"
#if !ARRAY_DEBUG #if !ARRAY_DEBUG
# define NDEBUG # define NDEBUG
@ -5342,39 +5343,11 @@ static ID id_random;
#define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1) #define RAND_UPTO(max) (long)rb_random_ulong_limited((randgen), (max)-1)
/*
* call-seq:
* ary.shuffle! -> ary
* ary.shuffle!(random: rng) -> ary
*
* Shuffles elements in +self+ in place.
*
* a = [ 1, 2, 3 ] #=> [1, 2, 3]
* a.shuffle! #=> [2, 3, 1]
* a #=> [2, 3, 1]
*
* The optional +rng+ argument will be used as the random number generator.
*
* a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
*/
static VALUE static VALUE
rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary) rb_ary_shuffle_bang(rb_execution_context_t *ec, VALUE ary, VALUE randgen)
{ {
VALUE opts, randgen = rb_cRandom;
long i, len; long i, len;
if (OPTHASH_GIVEN_P(opts)) {
VALUE rnd;
ID keyword_ids[1];
keyword_ids[0] = id_random;
rb_get_kwargs(opts, keyword_ids, 0, 1, &rnd);
if (rnd != Qundef) {
randgen = rnd;
}
}
rb_check_arity(argc, 0, 0);
rb_ary_modify(ary); rb_ary_modify(ary);
i = len = RARRAY_LEN(ary); i = len = RARRAY_LEN(ary);
RARRAY_PTR_USE(ary, ptr, { RARRAY_PTR_USE(ary, ptr, {
@ -5392,28 +5365,11 @@ rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
return ary; return ary;
} }
/*
* call-seq:
* ary.shuffle -> new_ary
* ary.shuffle(random: rng) -> new_ary
*
* Returns a new array with elements of +self+ shuffled.
*
* a = [ 1, 2, 3 ] #=> [1, 2, 3]
* a.shuffle #=> [2, 3, 1]
* a #=> [1, 2, 3]
*
* The optional +rng+ argument will be used as the random number generator.
*
* a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
*/
static VALUE static VALUE
rb_ary_shuffle(int argc, VALUE *argv, VALUE ary) rb_ary_shuffle(rb_execution_context_t *ec, VALUE ary, VALUE randgen)
{ {
ary = rb_ary_dup(ary); ary = rb_ary_dup(ary);
rb_ary_shuffle_bang(argc, argv, ary); rb_ary_shuffle_bang(ec, ary, randgen);
return ary; return ary;
} }
@ -7047,8 +7003,6 @@ Init_Array(void)
rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1); rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1); rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
rb_define_method(rb_cArray, "count", rb_ary_count, -1); rb_define_method(rb_cArray, "count", rb_ary_count, -1);
rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, -1);
rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, -1);
rb_define_method(rb_cArray, "sample", rb_ary_sample, -1); rb_define_method(rb_cArray, "sample", rb_ary_sample, -1);
rb_define_method(rb_cArray, "cycle", rb_ary_cycle, -1); rb_define_method(rb_cArray, "cycle", rb_ary_cycle, -1);
rb_define_method(rb_cArray, "permutation", rb_ary_permutation, -1); rb_define_method(rb_cArray, "permutation", rb_ary_permutation, -1);
@ -7074,3 +7028,5 @@ Init_Array(void)
id_random = rb_intern("random"); id_random = rb_intern("random");
} }
#include "array.rbinc"

36
array.rb Executable file
Просмотреть файл

@ -0,0 +1,36 @@
#!/usr/bin/ruby
class Array
# call-seq:
# ary.shuffle! -> ary
# ary.shuffle!(random: rng) -> ary
#
# Shuffles elements in +self+ in place.
#
# a = [ 1, 2, 3 ] #=> [1, 2, 3]
# a.shuffle! #=> [2, 3, 1]
# a #=> [2, 3, 1]
#
# The optional +rng+ argument will be used as the random number generator.
#
# a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
def shuffle!(random: Random)
__builtin_rb_ary_shuffle_bang(random)
end
# call-seq:
# ary.shuffle -> new_ary
# ary.shuffle(random: rng) -> new_ary
#
# Returns a new array with elements of +self+ shuffled.
#
# a = [ 1, 2, 3 ] #=> [1, 2, 3]
# a.shuffle #=> [2, 3, 1]
# a #=> [1, 2, 3]
#
# The optional +rng+ argument will be used as the random number generator.
#
# a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
def shuffle(random: Random)
__builtin_rb_ary_shuffle(random);
end
end

Просмотреть файл

@ -1001,6 +1001,7 @@ BUILTIN_RB_SRCS = \
$(srcdir)/pack.rb \ $(srcdir)/pack.rb \
$(srcdir)/trace_point.rb \ $(srcdir)/trace_point.rb \
$(srcdir)/warning.rb \ $(srcdir)/warning.rb \
$(srcdir)/array.rb \
$(srcdir)/prelude.rb \ $(srcdir)/prelude.rb \
$(srcdir)/gem_prelude.rb \ $(srcdir)/gem_prelude.rb \
$(empty) $(empty)
@ -1601,7 +1602,9 @@ array.$(OBJEXT): $(top_srcdir)/internal/stdbool.h
array.$(OBJEXT): $(top_srcdir)/internal/vm.h array.$(OBJEXT): $(top_srcdir)/internal/vm.h
array.$(OBJEXT): $(top_srcdir)/internal/warnings.h array.$(OBJEXT): $(top_srcdir)/internal/warnings.h
array.$(OBJEXT): {$(VPATH)}array.c array.$(OBJEXT): {$(VPATH)}array.c
array.$(OBJEXT): {$(VPATH)}array.rbinc
array.$(OBJEXT): {$(VPATH)}assert.h array.$(OBJEXT): {$(VPATH)}assert.h
array.$(OBJEXT): {$(VPATH)}builtin.h
array.$(OBJEXT): {$(VPATH)}config.h array.$(OBJEXT): {$(VPATH)}config.h
array.$(OBJEXT): {$(VPATH)}debug_counter.h array.$(OBJEXT): {$(VPATH)}debug_counter.h
array.$(OBJEXT): {$(VPATH)}defines.h array.$(OBJEXT): {$(VPATH)}defines.h

Просмотреть файл

@ -83,6 +83,7 @@ rb_call_inits(void)
BUILTIN(trace_point); BUILTIN(trace_point);
BUILTIN(pack); BUILTIN(pack);
BUILTIN(warning); BUILTIN(warning);
BUILTIN(array);
Init_builtin_prelude(); Init_builtin_prelude();
} }
#undef CALL #undef CALL

Просмотреть файл

@ -25,10 +25,13 @@ describe "Array#shuffle" do
ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(Array) ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(Array)
end end
it "attempts coercion via #to_hash" do ruby_version_is ""..."2.8" do
obj = mock('hash') # keyword argument since 2.8
obj.should_receive(:to_hash).once.and_return({}) it "attempts coercion via #to_hash" do
[2, 3].shuffle(obj) obj = mock('hash')
obj.should_receive(:to_hash).once.and_return({})
[2, 3].shuffle(obj)
end
end end
it "calls #rand on the Object passed by the :random key in the arguments Hash" do it "calls #rand on the Object passed by the :random key in the arguments Hash" do