summaryrefslogtreecommitdiff
path: root/array.c
AgeCommit message (Collapse)Author
2019-01-15Clarify Array#- and Array#difference documentationnobu
Currently we are not explicit enough regarding the potentially confusing behavior of `Array#-` and `Array#difference` when it comes to duplicate items within receiver arrays. Although the original documentation for these methods does use an array with multiple instance of the same integers, the explanation for the behavior is actually imprecise. > removing any items that also appear in +other_ary+ Not only does `Array#-` remove any items that also appear in `other_ary` but it also remove any instance of any item in `other_ary`. One may expect `Array#-` to behave like mathematical subtraction or difference when it doesn't. One could be forgiven to expect the following behavior: ```ruby [1,1,2,2,3,3,4,4] - [1,2,3,4] => [1,2,3,4] ``` In reality this is the result: ```ruby [1,1,2,2,3,3,4,4] - [1,2,3,4] => [] ``` I hope that I've prevented this potential confusion with the clarifications in this change. I can offer this as evidence of likeliness for confusion: https://twitter.com/olivierlacan/status/1084930269533085696 I'll freely admit I was surprised by this behavior myself since I needed to obtain an Array with only one instance of each item in the argument array removed. [Fix GH-2068] [ci skip] From: Olivier Lacan <hi@olivierlacan.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-09Fix styles [ci skip]nobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-08Mark array as "going to be modified" in `Array#reject!`tenderlove
Before this patch, if `reject!` is called on a shared array it can mutate the shared array rather than a copy. This patch marks the array as "going to be modified" so that the shared source array isn't mutated. [Bug #15479] [ruby-core:90781] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-23fix marking T_NONE object bug.ko1
* array.c (rb_ary_splice): do not use RARRAY_PTR() here because it can cause GC because of rb_ary_detransient(). Here ary can contain T_NONE object because of increasing capacity and not initialized yet. error log: http://ci.rvm.jp/results/trunk-test@ruby-sky1/1557174 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-14rename li_table->ar_table (and related names).ko1
* internal.h: rename the following names: * li_table -> ar_table. "li" means linear (from linear search), but we use the word "array" (from data layout). * RHASH_ARRAY -> RHASH_AR_TABLE. AR_TABLE is more clear. * rb_hash_array_* -> rb_hash_ar_table_*. * RHASH_TABLE_P() -> RHASH_ST_TABLE_P(). more clear. * RHASH_CLEAR() -> RHASH_ST_CLEAR(). * hash.c: rename "linear_" prefix functions to "ar_" prefix. * hash.c (linear_init_table): rename to ar_alloc_table. * debug_counter.h: rename obj_hash_array to obj_hash_ar. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66390 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-05implement Array-specific #all?, #none?, #one?nobu
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
2018-12-05Prefer rb_check_arity when 0 or 1 argumentsnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-04Fix error messagenobu
* array.c (ary_take_first_or_last): expected optional argument. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66190 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-04Prefer rb_check_arity when 0 or 1 argumentsnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66179 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-03* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66166 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-03make `RARRAY_PTR_USE` more conservertive.ko1
* include/ruby/ruby.h: de-transient at `RARRAY_PTR_USE` and `RARRAY_PTR_USE_START`. Introduce `RARRAY_PTR_USE_TRANSIENT` and `RARRAY_PTR_USE_START_TRANSIENT` if you don't want to de-transient an array. Generally, it is difficult so C-extension writers should not use them. * array.c: use `RARRAY_PTR_USE_TRANSIENT` if possible. * hash.c: ditto. * enum.c (enum_sort_by): remove `rb_ary_transient_heap_evacuate()` because `RARRAY_PTR_USE` do de-transient. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66165 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-29Use a shared array for the `duparray` instructiontenderlove
In this example code: ```ruby def foo [1, 2, 3, 4] end ``` The array literal uses a `duparray` instruction. Before this patch, `rb_ary_resurrect` would malloc and memcpy a new array buffer. This patch changes `rb_ary_resurrect` to use `ary_make_partial` so that the new array object shares the underlying buffer with the array stored in the instruction sequences. Before this patch, the new array object is not shared: ``` $ ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])' "{\"address\":\"0x7fa2718372d0\", \"type\":\"ARRAY\", \"class\":\"0x7fa26f8b0010\", \"length\":4, \"memsize\":72, \"flags\":{\"wb_protected\":true}}\n" ``` After this patch: ``` $ ./ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])' "{\"address\":\"0x7f9a76883638\", \"type\":\"ARRAY\", \"class\":\"0x7f9a758af900\", \"length\":4, \"shared\":true, \"references\":[\"0x7f9a768837c8\"], \"memsize\":40, \"flags\":{\"wb_protected\":true}}\n" ``` [Feature #15289] [ruby-core:90097] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22Revert r63030nobu
* array.c (rb_ary_collect): no longer splat sole array for lambda. [ruby-core:89734] [Bug #15285] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65923 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-19array.c: avoid (VALUE)--shyouhei
This args[1]-- overflows when it is zero. Should do that only when we can say it is nonzero. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65798 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-04array.c: [DOC] add docs for Array#{filter,filter!}stomar
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65528 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-04array.c: [DOC] improve Array#{select,select!,keep_if} docsstomar
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-01introduce USE_TRANSIENT_HEAP to enable/disable theap.ko1
* include/ruby/ruby.h: intrdocue `USE_TRANSIENT_HEAP` macro to enable/disable transient heap. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30* remove trailing spaces, expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65455 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30support theap for T_HASH. [Feature #14989]ko1
* hash.c, internal.h: support theap for small Hash. Introduce RHASH_ARRAY (li_table) besides st_table and small Hash (<=8 entries) are managed by an array data structure. This array data can be managed by theap. If st_table is needed, then converting array data to st_table data. For st_table using code, we prepare "stlike" APIs which accepts hash value and are very similar to st_ APIs. This work is based on the GSoC achievement by tacinight <tacingiht@gmail.com> and refined by ko1. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30introduce TransientHeap. [Bug #14858]ko1
* transient_heap.c, transient_heap.h: implement TransientHeap (theap). theap is designed for Ruby's object system. theap is like Eden heap on generational GC terminology. theap allocation is very fast because it only needs to bump up pointer and deallocation is also fast because we don't do anything. However we need to evacuate (Copy GC terminology) if theap memory is long-lived. Evacuation logic is needed for each type. See [Bug #14858] for details. * array.c: Now, theap for T_ARRAY is supported. ary_heap_alloc() tries to allocate memory area from theap. If this trial sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on. We don't need to free theap ptr. * ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that if ary is allocated at theap, force evacuation to malloc'ed memory. It makes programs slow, but very compatible with current code because theap memory can be evacuated (theap memory will be recycled). If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT() instead of RARRAY_CONST_PTR(). If you can't understand when evacuation will occur, use RARRAY_CONST_PTR(). (re-commit of r65444) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30revert r65444 and r65446 because of commit missko1
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65447 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65445 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30introduce TransientHeap. [Bug #14858]ko1
* transient_heap.c, transient_heap.h: implement TransientHeap (theap). theap is designed for Ruby's object system. theap is like Eden heap on generational GC terminology. theap allocation is very fast because it only needs to bump up pointer and deallocation is also fast because we don't do anything. However we need to evacuate (Copy GC terminology) if theap memory is long-lived. Evacuation logic is needed for each type. See [Bug #14858] for details. * array.c: Now, theap for T_ARRAY is supported. ary_heap_alloc() tries to allocate memory area from theap. If this trial sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on. We don't need to free theap ptr. * ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that if ary is allocated at theap, force evacuation to malloc'ed memory. It makes programs slow, but very compatible with current code because theap memory can be evacuated (theap memory will be recycled). If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT() instead of RARRAY_CONST_PTR(). If you can't understand when evacuation will occur, use RARRAY_CONST_PTR(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30use RARRAY_AREF() instead of RARRAY_CONST_PTR().ko1
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-19array.c, file.c, string.c: [DOC] fix typosstomar
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65185 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-19array.c: improve docs for Array#differencestomar
* array.c: [DOC] small doc fixes for Array#difference and Array#-. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65184 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-13array.c: fix rdoc syntaxstomar
* array.c: [DOC] use `<code>other_ary</code>s' instead of `+other_ary+s', which is not rendered correctly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65066 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-10revisit `RARRAY_PTR()`.ko1
* array.c (yield_indexed_values): use RARRAY_AREF/ASET instead of using RARRAY_PTR(). * enum.c (nmin_filter): ditto. * proc.c (rb_sym_to_proc): ditto. * enum.c (rb_nmin_run): use RARRAY_PTR_USE() instead of RARRAY_PTR(). It is safe because they don't make new referecen from an array. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64986 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-10revisit `RARRAY_PTR()`.ko1
* array.c (ary_memcpy0): remove traditional `RARRAY_PTR()` code. It's enough stable. * array.c (rb_ary_splice): add comment about wb-unprotect. * array.c (rotate_count): use `RARRAY_PTR_USE()` instead of `RARRAY_PTR()` to avoid wb-unprotect. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-07Fix indent of output in doc [ci skip]kazu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64930 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-05* expand tabs.svn
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-05Add difference method to Arraynobu
I introduce a `difference` method equivalent to the `-` operator, but which accept more than array as argument. This improved readability, and it is also coherent with the `+` operator, which has a similar `concat` method. The method doesn't modify the original object and return a new object instead. I plan to introduce a `difference!` method as well. Tests and documentation are included. It solves partially https://bugs.ruby-lang.org/issues/14097 From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64921 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-21Fix sample code [ci skip]kazu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20Enumerable#to_h with block and so onnobu
[Feature #15143] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20Introduce rb_ary_union_hash method in Arraynobu
Avoid repeating code and improve readability in `rb_ary_or` and `rb_ary_union_multi`. Similaty as done with `rb_ary_union`. [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64790 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20Link Array#union from | methodnobu
`Array#uniq` is not really related with `Array#|`, so I replaced it by `Array#union`. [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20Introduce rb_ary_union method in Arraynobu
Avoid repeating code and improve readability in `rb_ary_or` and `rb_ary_union_multi`. [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20Add union method to Arraynobu
I introduce a `union` method equivalent to the `|` operator, but which accept more than array as argument. This improved readability, and it is also coherent with the `+` operator, which has a similar `concat` method. The method doesn't modify the original object and return a new object instead. It is plan to introduce a `union!` method as well. Tests and documentation are included. It solves partially https://bugs.ruby-lang.org/issues/14097 [Fix GH-1747] [Feature #14097] From: Ana María Martínez Gómez <ammartinez@suse.de> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-16array.c: Optimize rb_ary_and. Patch by Stefan Schüßler. [Fix GH-1938]marcandre
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64755 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-13warn unused blocks with Enumerable#all? any? one? none?nobu
[Fix GH-1953] From: Koji Onishi <fursich0@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64733 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-28rest parameter optimization [Feature #15010]ko1
* vm_args.c: rb_ary_dup(args->rest) to be used at most once during parameter setup. [Feature #15010] A patch by chopraanmol1 (Anmol Chopra) <chopraanmol1@gmail.com>. * array.c (rb_ary_behead): added to remove first n elements. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-22fix sum on infinitynobu
* array.c (rb_ary_sum): consider non-finite floats. [ruby-core:88024] [Bug #14926] * enum.c (sum_iter): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64014 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-11Make block spacing consistentnobu
[Fix GH-1910] [ci skip] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-01Fix a typo [ci skip]kazu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63814 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-23[DOC] Add call signature for {Array,Hash}#any?ktsj
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63729 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-21array.c: use ruby_sized_free and SIZED_REALLOC_Nnormal
Part of the plan to reduce dependencies on malloc_usable_size which costs us speed: https://bugs.ruby-lang.org/issues/10238 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63481 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-08rb_ary_dig, rb_hash_dig: nobody is using them outside.shyouhei
mark them static. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63358 b2dd03c8-39d4-4d8f-98ff-823fe69b080e