Граф коммитов

764 Коммитов

Автор SHA1 Сообщение Дата
shyouhei 5069122ab6 refactor torexp to use routine in array.c
Found a part where copy&paste can be eliminated.  Reduces vm_exec_core
from 26,228 bytes to 26,176 bytes in size on my machine. I believe it
does not affect any runtime performance.

----

	* array.c (rb_ary_tmp_new_from_values): extend existing
          rb_ary_new_from_values function so that it can take
          additional value for klass.
	* array.c (rb_ary_new_from_values): use the new function.
	* insns.def (toregexp): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-20 10:32:08 +00:00
mrkn cb8012f5c0 array.c: improve performance of Array#sort with block
* array.c (sort_1): improve performance of Array#sort with block

* benchmark/bm_array_sort_block.rb: added for Array#sort with block

[Bug #13344]
[ruby-dev:50027]
[Fix GH-1544]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58339 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 09:25:38 +00:00
mrkn 1b5acc876b array.c: Improve performance of Array#sort with float elements
* array.c (sort_2): improve performance of Array#sort with float elements.

* internal.h (cmp_opt_Float, cmp_opt_data): added for checking whether or not
  Float#<=> can be optimizable.

* numeric.c (rb_float_cmp): added for internal use.

* internal.h (rb_float_cmp): ditto.

[Bug #13340]
[ruby-dev:50023]
[Fix GH-1539]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 07:22:35 +00:00
rhe 4e85feed33 array.c: report correct memsize for shared root arrays
For a shared array root, struct RArray::as.heap.aux.capa stores the
number of Arrays holding reference to that T_ARRAY instead of the actual
heap-allocated capacity. Use ARY_CAPA() macro which handles this
appropriately.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 06:43:17 +00:00
nobu f57d515d69 array.c: Array#append and Array#prepend
* array.c (Init_Array): Add alias "append" to Array#push, and
  "prepend" to Array#unshift.  [Feature #12746] [Fix GH-1574]

Author:    pascbjumper2 <stowers.joshua@live.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-01 07:38:12 +00:00
stomar 56abb1cdd8 docs for creating arrays
* array.c: [DOC] add example for Array.new with block and index.
  Reported by Don Cruickshank.  [ruby-core:68442] [Bug #10944]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58037 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-20 20:30:25 +00:00
stomar b4baeb5af6 documentation for sort methods
* array.c: [DOC] fix grammar in Array#sort, #sort!, #sort_by!,
  move references below the code example, add a missing reference.
* enum.c: [DOC] fix grammar in Enumerable#sort, #sort_by.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57771 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-04 13:13:50 +00:00
nobu 3203ae53ff array.c: check if numeric
* array.c (finish_exact_sum): add 0 and the initial value to check
  if the latter is numeric.  [ruby-core:79572] [Bug #13222]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-18 04:23:20 +00:00
nobu 7dccda437b array.c: finish_exact_sum
* array.c (finish_exact_sum): extract duplicate code from
  rb_ary_sum.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-18 03:42:35 +00:00
normal 302ae913e0 array.c (ary_recycle_hash): use rb_gc_force_recycle
Hidden objects (RBASIC_CLASS(hash) == 0) can never become
visible to other threads or signal handlers via
ObjectSpace.each_object or similar means.  Thus it is safe to
forcibly recycle the object slot for future use, here.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57615 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-13 02:04:26 +00:00
nobu 2de1dbdffc array.c: improve Array#sample
* array.c (rb_ary_sample): improve performance when many samples
  from a large array.  based on the patch by tomoya ishida
  <tomoyapenguin AT gmail.com> in [ruby-dev:49956].  [Bug #13136]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-20 02:39:27 +00:00
normal eeb36c5c74 mention behavior of Array#join for nested arrays [ci skip]
The current documentation for Array#join does not mention the
special treatment of nested arrays.

It says:
> Returns a string created by converting each element of the
> array to a string, separated by the given separator.

Expected behavior according to the docs would be:

    [ "a", [1, 2, [:x, :y]], "b" ].join("-")  #=> "a-[1, 2, [:x, :y]]-b"
    # because of:
    [1, 2, [:x, :y]].to_s  #=> "[1, 2, [:x, :y]]"

Actual behavior:

    [ "a", [1, 2, [:x, :y]], "b" ].join("-")  #=> "a-1-2-x-y-b"

because join is applied recursively for nested arrays.

The patch clarifies this behavior.

(Also: small markup and grammar fix.)

Patch by Marcus Stollsteimer <sto.mar@web.de>

[ruby-talk:437238] [ruby-core:79079] [Bug #13130]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57329 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-14 23:09:55 +00:00
rhe b5da45d6d7 array.c: do not resize to less than 0
Shrinking the Array from the block invoked by Array#select! or
Array#reject! causes the Array to be a negative number size. Ensure that
the resulting Array won't be smaller than 0.
[ruby-core:78739] [Bug #13053]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57121 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-20 06:53:44 +00:00
rhe 647ba111ea array.c: check array length every time after yielding
Since the Array may be modified during rb_yield(), the length before
invoking the block can't be trusted. Fix possible out-of-bounds read in
Array#combination and Array#repeated_combination.

It may better to make a defensive copy of the Array, but for now let's
follow what Array#permutation does.  [ruby-core:78738] [Bug #13052]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57119 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-20 05:26:08 +00:00
mrkn 48f5f5915b array.c, enum.c: change sum algorithm
* array.c (rb_ary_sum): change the algorithm to Kahan-Babuska balancing
  summation to be more precise.
  [Feature #12871] [ruby-core:77771]

* enum.c (sum_iter, enum_sum): ditto.

* test_array.rb, test_enum.rb: add an assertion for the above change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-06 13:40:31 +00:00
a_matsuda f12003d372 Update documentation of fetch
The sentence `Negative values of +index+ count from the end of the array.` can be interpreted that it only holds if a block is given. Clarify it.

Patch by: Lukas Elmer <lukas.elmer@gmail.com> (@lukaselmer)
Signed-off-by: Akira Matsuda <ronnie@dio.jp>

closes #1472
[ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-08 19:43:58 +00:00
akr 8afd5857f0 [DOC] add explanation for Array#sum
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-05 02:50:15 +00:00
nobu 5b3b8554c9 unstable sort [ci skip]
* array.c (rb_ary_sort_bang, rb_ary_sort, rb_ary_sort_by_bang):
  [DOC] describe that sort may not be stable.
* enum.c (enum_sort, enum_sort_by): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56413 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-13 02:35:34 +00:00
nobu dd2ebf4d78 replace Fixnum with Integer in rdoc [ci skip]
* array.c, class.c: Fixed documentation where Fixnum was referred
  directly to use Integer, as Fixnum and Bignum are now unified
  into Integer and direct usage is deprecated.  [Fix GH-1459]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56382 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-09 14:05:57 +00:00
usa c2dd2d268e * internal.h (ST2FIX): new macro to convert st_index_t to Fixnum.
a hash value of Object might be Bignum, but it causes many troubles
  expecially the Object is used as a key of a hash.  so I've gave up
  to do so.

* array.c (rb_ary_hash): use above macro.

* bignum.c (rb_big_hash): ditto.

* hash.c (rb_obj_hash, rb_hash_hash): ditto.

* numeric.c (rb_dbl_hash): ditto.

* proc.c (proc_hash): ditto.

* re.c (rb_reg_hash, match_hash): ditto.

* string.c (rb_str_hash_m): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56340 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-04 16:25:01 +00:00
nobu e71e83b021 array.c: update Array#dig doc
* array.c (rb_ary_dig): [DOC] update an example of error message
  by Array#dig, because of Integer Unification.  [Fix GH-1455]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-04 03:59:46 +00:00
nobu 0cc169d1de fid typos [ci skip]
* fix typos, "a" before "Integer" to "an".  [Fix GH-1438]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-24 02:28:25 +00:00
ktsj 1f8765bb2b * array.c (flatten): use rb_obj_class instead of rb_class_of
because rb_class_of may return a singleton class.
  [ruby-dev:49781] [Bug #12738]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-08 08:56:30 +00:00
akr 577de1e93d replace fixnum by integer in documents.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-08 04:57:49 +00:00
nobu 9387ff7315 multiple arguments
* array.c (rb_ary_concat_multi): take multiple arguments.  based
  on the patch by Satoru Horie.  [Feature #12333]
* string.c (rb_str_concat_multi, rb_str_prepend_multi): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-27 01:26:17 +00:00
nobu 2329d8b0de array.c: elements in self
* array.c (rb_ary_splice): consider elements in middle of self.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55983 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-22 04:21:54 +00:00
nobu 4831eb86f6 array.c: no temporary array
* array.c (rb_ary_splice): use pointer and length pair instead of
  an array object to replace.

* array.c (rb_ary_insert): get rid of creating temporary array.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55977 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-21 04:03:22 +00:00
nobu aa0e51b529 hash.c: rb_hash_add_new_element
* hash.c (rb_hash_add_new_element): add new element or do nothing
  if it is contained already.
* array.c (ary_add_hash, ary_add_hash_by): use
  rb_hash_add_new_element.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55707 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-07-20 08:35:25 +00:00
naruse cb2a99822e * array.c (rb_ary_fill): suppress warnings: 'item' may be used
uninitialized in this function

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-23 03:23:24 +00:00
naruse 6b4132724a * array.c (rb_ary_entry): extract rb_ary_elt to organize if-conditions
and check whether is is embdeded at once.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-15 07:17:46 +00:00
mrkn 78729a59a2 Fix rb_ary_sum for mathn
* array.c (rb_ary_sum): fix for mathn

* test/ruby/test_array.rb (test_sum): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54870 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-01 15:02:47 +00:00
akr ef6af0c967 [DOC] Arrah#sum with non-numeric objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54705 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-22 11:44:34 +00:00
ktsj 5e08452993 * array.c (rb_ary_sum): [DOC] fix typos.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-17 11:34:52 +00:00
akr b59a158a18 * array.c (rb_ary_sum): Don't yield same element twice.
Found by nagachika.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54609 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 16:26:38 +00:00
akr 72305d2531 * array.c (rb_ary_sum): Fix SEGV by [1/2r, 1].sum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 16:04:18 +00:00
mrkn d1d95056a3 rename rb_rational_add -> rb_rational_plus
* rational.c (rb_rational_plus): rename from rb_rational_add
  to be aligned with rb_fix_plus.

* array.c (rb_ary_sum): ditto.

* internal.h: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 14:54:39 +00:00
mrkn 0a267b79f6 array.c (rb_ary_sum): use rb_rational_add directly
* rational.c (rb_rational_add): rename from nurat_add.

* array.c (rb_ary_sum): use rb_rational_add directly.

* test/ruby/test_array.rb (test_sum): add assertions for an array of
  Rational values.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 14:46:35 +00:00
mrkn a945eb9dda array.c: sum for Rational and Float mixed arrays
* array.c (rb_ary_sum): apply the precision compensated algorithm
  for an array in which Rational and Float values are mixed.

* test/ruby/test_array.rb (test_sum): add assertions for the above
  change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 13:33:05 +00:00
akr df941c91b1 [DOC]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54578 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 12:56:38 +00:00
akr 76426c7ea5 * array.c (rb_ary_sum): Support the optional argument, init, and block.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54577 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 12:48:44 +00:00
akr 350a740058 [DOC]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 00:00:32 +00:00
akr b359d20352 * array.c (rb_ary_sum): Array#sum is implemented.
Kahan's compensated summation algorithm for precise sum of float
  numbers is moved from ary_inject_op in enum.c.

* enum.c (ary_inject_op): Don't specialize for float numbers.

  [ruby-core:74569] [Feature#12217] proposed by mrkn.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-13 13:51:53 +00:00
mame b3a65c883a * array.c, enum.c: make rdoc format consistent.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:52:47 +00:00
mame 4ca0483a28 * array.c (rb_ary_max, rb_ary_min): implement Array#max and min with
arguments.  replace super call with rb_nmin_run.

* enum.c (nmin_run): exported (as rb_nmin_run).

* internal.h: added a prototype for rb_nmin_run.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54152 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:37:20 +00:00
mame 90ab5beb42 * array.c (rb_ary_max, rb_ary_min): implement a block by itself instead
of delegating Enumerable#max/min.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:25:40 +00:00
mame 68a6f2e9e0 * array.c (rb_ary_max, rb_ary_min): Array#max and Array#min added.
[Feature #12172]

* internal.h (OPTIMIZED_CMP): moved from enum.c so that array.c can
  use it.

* test/ruby/test_array.rb (test_max, test_min): tests for Array#max
  and Array#min.

* test/ruby/test_enum.rb (test_max, test_min): revised a bit to test
  Enumerable#max and #min explicitly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54150 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:14:21 +00:00
mame a22455199b * internal.c: struct cmp_opt_data added for refactoring out a data
structure for CMP_OPTIMIZABLE

* array.c (struct ary_sort_data): use struct cmp_opt_data.

* enum.c (struct min_t, max_t, min_max_t): use struct cmp_opt_data.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54149 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:03:48 +00:00
nobu c31d06eeb7 array.c: [DOC] remove trailing comma [ci skip]
* array.c (rb_ary_push_m): [DOC] Remove trailing comma from
  Array#push example, as other Array examples doesn't put trailing
  comma.  [Fix GH-1279]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-02-29 07:51:15 +00:00
nobu 491c44e984 Clarify set intersection and union documentation
* array.c (rb_ary_and): clarify that set intersection returns the
  unique elements common to both arrays.

* array.c (rb_ary_or): clarify that union preserves the order from
  the given arrays.

- Most know what intersection means, but saying the operation
  excludes duplicates could be misleading ([1] & [1], duplicates
  excluded, might mean a result of []).
- Instead, saying intersection returns the unique elements common to both
  arrays is more concise and less ambiguous.
- The set union's documentation was incomplete in its describing
  preservation of order. Saying union preserves the order of the
  original array neglects the idea that the order of the elements
  in both arrays, as given, will be preserved.
- Instead, saying set union preserves the order from the given arrays (and
  adding an example) fully demonstrates the idea.

[Fix GH-1273] [ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53958 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-02-28 01:20:39 +00:00
nobu 4df168074c Fix a Ruby-Doc comment for Array#dig
* array.c (rb_ary_dig): [DOC] fix the exception class to be raised
  when intermediate object does not have dig method.  TypeError
  will be raised now.  [Fix GH-1224]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53666 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-27 07:26:53 +00:00