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

364 Коммитов

Автор SHA1 Сообщение Дата
Nobuyoshi Nakada cd0d4ec059
Single letter ASCII operator is same as the corresponding ID 2024-08-19 19:50:08 +09:00
Victor Shepelev d450f9d6a2
Make Range#step to consistently use + for iteration (#7444)
Make Range#step to consistently use + for iteration [Feature #18368]

Previously, non-numerics expected step to be integer,
and iterated with begin#succ, skipping over step value
steps. Since this commit, numeric and non-numeric iteration
behaves the same way, by using + operator.
2024-08-18 13:15:18 +03:00
Iskren 371790165f
[DOC] Fix and improve array slicing example in range.c
* [DOC] Fix typo in range.c

  In the example of the beginless range used for array slicing,
  '..' range literal was used while the '...' literal was expected

* [DOC] Add example for array slicing in range.c

  Add an example for the array slice with the beginless range
  using the '..' range literal

* [DOC] Add comments for array slicing in range.c

  Add comments to make crystal clear what the '..' and '...'
  range literals do when used for array slicing as beginless range
2024-07-17 11:42:29 +09:00
Kouhei Yanagita 9f6deaa688 [Misc #18984] Raise TypeError from Range#size if the range is not iterable 2024-04-10 07:28:07 -07:00
Nobuyoshi Nakada b176315827
[Bug #20324] Uncomparable ranges are not overlapping 2024-03-04 21:02:08 +09:00
Yusuke Endoh 25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu 3ed5962755 Replace assert with RUBY_ASSERT in range.c
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-12 15:07:47 -05:00
Peter Zhu 9ec4eae0b1 [DOC] Fix invalid syntax in Range#eql? 2023-12-27 19:31:56 -05:00
Kouhei Yanagita e8639098ed [Bug #19977] Fix (nil..nil) === x not to raise TypeError 2023-12-22 16:15:33 +09:00
Victor Shepelev 570d7b2c3e
[DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02:00
Victor Shepelev 07734b51c6
[DOC] Small fixes for documentation rendering
Mostly just fixing RDoc's incorrect treatment of `+`
2023-12-09 13:54:33 +09:00
Kouhei Yanagita 1a16b6ffc2 Make Range#reverse_each raise TypeError if endless 2023-11-28 15:10:42 +09:00
Burdette Lamar b92a92a3ff
[DOC] More on JSON extensions (#8898) 2023-11-16 08:28:07 -08:00
Tanaka Akira c23b25f75f describe the assumption for Range#overlap?.
Range#overlap? assumes that there is no minimum value.
This assumption makes
+(...-Float::INFINITY).overlap?((...-Float::INFINITY))+ returns true
while +(...-Float::INFINITY)+ is empty.
2023-10-13 00:00:58 +09:00
Kouhei Yanagita 66fabefa03 Add Range#reverse_each implementation for performance 2023-10-12 17:34:49 +09:00
Kouhei Yanagita 6ae2996e29
Optimize `Range#count` by using `range_size` if possible 2023-10-05 00:19:55 +09:00
Nobuyoshi Nakada 50520cc193
[DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Kouhei Yanagita 4199e49cad Optimize Range#bsearch by reducing the number of Integer#+ calls 2023-09-26 17:31:10 +09:00
Kouhei Yanagita 7e350f5310 Optimize Range#bsearch for beginless/endless ranges within Fixnum 2023-09-21 10:30:58 +09:00
Jeremy Evans 25711683e8 Fix regression when testing inclusion in unbounded ranges
Caused by 04a92a6764.  This treats
unbounded ranges of arbitrary objects the same as how unbounded
string ranges are treated:

  (..x)  === y  # (y <=> x) <= 0
  (...x) === y  # (y <=> x) <  0
  (x..)  === y  # (x <=> y) <= 0

Fixes [Bug #19864]
2023-09-16 16:02:31 +01:00
Nobuyoshi Nakada e779465f19
Add comment markers in empty lines [ci skip] 2023-09-16 22:13:19 +09:00
Nobuyoshi Nakada b4213a73b8 [Feature #19839] Fix `Range#overlap?` for empty ranges
Empty ranges do not overlap with any range.

Regarding benchmarks, PR#8242 is significantly faster in some cases,
but one of these two cases is a wrong result.

|                           |ActiveSupport| PR#8242|built-ruby|
|:--------------------------|------------:|-------:|---------:|
|(2..3).overlap?(1..1)      |       7.761M| 15.053M|   32.368M|
|                           |            -|   1.94x|     4.17x|
|(2..3).overlap?(2..4)      |      25.720M| 55.070M|   21.981M|
|                           |        1.17x|   2.51x|         -|
|(2..3).overlap?(4..5)      |       7.616M| 15.048M|   21.730M|
|                           |            -|   1.98x|     2.85x|
|(2..3).overlap?(2..1)      |      25.585M| 56.545M|   32.786M|
|                           |            -|   2.21x|     1.28x|
|(2..3).overlap?(0..1)      |       7.554M| 14.755M|   32.545M|
|                           |            -|   1.95x|     4.31x|
|(2..3).overlap?(...1)      |       6.681M|  5.843M|   32.255M|
|                           |        1.14x|       -|     5.52x|
|(2...3).overlap?(..2)      |       6.676M|  5.817M|   21.572M|
|                           |        1.15x|       -|     3.71x|
|(2...3).overlap?(3...)     |       7.392M| 14.755M|   31.805M|
|                           |            -|   2.00x|     4.30x|
|(2..3).overlap?('a'..'d')  |       3.675M|  3.482M|   17.009M|
|                           |        1.06x|       -|     4.89x|
2023-09-16 17:24:21 +09:00
Shouichi Kamiya e9b503f1bb [Feature #19839] Add Range#overlap?
Add a method that returns true if two range overlap, otherwise false.

```
(0..10).overlap?(5..15) #=> true
(0..10).overlap?(20..30) #=> false
```
2023-09-16 14:57:19 +09:00
Kouhei Yanagita 7d08dbd015
Optimize Range#bsearch for beginless/endless ranges
On Range#bsearch for endless ranges, we try positions at `begin + 2**i` (i = 0, 1, 2, ...)
to find a point that satisfies a given condition.
Subsequently, we perform binary searching with the interval `[begin, begin + 2**n]`.

However, the interval `[begin + 2**(n-1), begin + 2**n]` is sufficient for binary search
because `begin + 2**(n-1)` does not satisfy the condition.

The same applies to beginless ranges.
2023-09-16 12:10:09 +09:00
Nobuyoshi Nakada fb17c833f5 [Bug #19533] Fix infinite range inclusion with numeric value 2023-04-14 10:22:09 +09:00
Nobuyoshi Nakada 1a149aab77 Extract range type check functions 2023-04-14 10:22:09 +09:00
BurdetteLamar 3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00
Nobuyoshi Nakada da4464b824
[Bug #19426] Fix endless `Range#step` with `#succ` method 2023-02-09 20:37:34 +09:00
Marco Costa d2847009db
[DOC] Fix most of Range#cover? marked as verbatim 2022-12-23 11:12:16 +09:00
Daniel Colson e69b91fae4 Introduce BOP_CMP for optimized comparison
Prior to this commit the `OPTIMIZED_CMP` macro relied on a method lookup
to determine whether `<=>` was overridden. The result of the lookup was
cached, but only for the duration of the specific method that
initialized the cmp_opt_data cache structure.

With this method lookup, `[x,y].max` is slower than doing `x > y ?
x : y` even though there's an optimized instruction for "new array max".
(John noticed somebody a proposed micro-optimization based on this fact
in https://github.com/mastodon/mastodon/pull/19903.)

```rb
a, b = 1, 2
Benchmark.ips do |bm|
  bm.report('conditional') { a > b ? a : b }
  bm.report('method') { [a, b].max }
  bm.compare!
end
```

Before:

```
Comparison:
         conditional: 22603733.2 i/s
              method: 19820412.7 i/s - 1.14x  (± 0.00) slower
```

This commit replaces the method lookup with a new CMP basic op, which
gives the examples above equivalent performance.

After:

```
Comparison:
              method: 24022466.5 i/s
         conditional: 23851094.2 i/s - same-ish: difference falls within
error
```

Relevant benchmarks show an improvement to Array#max and Array#min when
not using the optimized newarray_max instruction as well. They are
noticeably faster for small arrays with the relevant types, and the same
or maybe a touch faster on larger arrays.

```
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_min
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_max
```

The benchmarks added in this commit also look generally improved.

Co-authored-by: John Hawthorn <jhawthorn@github.com>
2022-12-06 12:37:23 -08:00
Jeremy Evans 04a92a6764 Raise TypeError for endless non-numeric range include?
Beginless ranges previously raised TypeError for this case,
except for string ranges, which had unexpected behavior:

  ('a'..'z').include?('ww') # false
  (..'z').include?('ww') # previously true, now TypeError

Use of include? with endless ranges could previously result
in an infinite loop.

This splits off a range_string_cover_internal function from
range_include_internal.

Fixes [Bug #18580]
2022-11-24 15:18:44 -08:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Yusuke Endoh e026368061 Range#size returns nil for (.."a") and (nil..)
Fixes [Bug #18983]
2022-10-21 16:35:46 +09:00
Jean Boussier bbe5ec7846 rb_int_range_last: properly handle non-exclusive range
[Bug #18994]
2022-09-04 11:16:11 +02:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Jeremy Evans c5475f4269 Fix Range#cover? returning true for beginless ranges of different types
Previously `(2..).cover?("2"..)` was false, but
`(..2).cover?(.."2")` was true.  This changes it so both are false,
treating beginless ranges the same as endless ranges in regards to
type checks.

This also adds documentation to #cover? to describe behavior with
beginless and endless ranges, testing each documentation example,
which is how this bug was found.

Fixes [Bug #18155]
2022-06-06 09:59:22 -07:00
Jeremy Evans f8724987db Document beginless, endless ranges in Range class documentation 2022-04-25 13:07:35 -07:00
Burdette Lamar ffcdbedbfb
Repaired What's Here sections for Range, String, Symbol, Struct (#5735)
Repaired What's Here sections for Range, String, Symbol, Struct.
2022-03-30 13:46:24 -05:00
Nobuyoshi Nakada 50c972a1ae
[DOC] Simplify operator method references 2022-02-12 12:38:36 +09:00
Jeremy Evans fd710d7e99 Fix Range#include? for beginless exclusive string ranges
Previously, include? would return true for the end of the range,
when it should return false because the range is exclusive.

Research and Analysis by Victor Shepelev.

Fixes [Bug #18577]
2022-02-09 19:47:28 -08:00
Nobuyoshi Nakada 8ca7b0b68a
[DOC] Fix broken links to operator methods
Once https://github.com/ruby/rdoc/pull/865 is merged, these hacks
are no longer needed.
2022-02-08 01:39:37 +09:00
Nobuyoshi Nakada 16fdc1ff46
[DOC] Fix broken links to literals.rdoc 2022-02-08 01:27:52 +09:00
Peter Zhu a32e5e1b97 [DOC] Use RDoc link style for links in the same class/module
I used this regex:

(?<=\[)#(?:class|module)-([A-Za-z]+)-label-([A-Za-z0-9\-\+]+)

And performed a global find & replace for this:

rdoc-ref:$1@$2
2022-02-07 09:52:06 -05:00
Peter Zhu f9a2802bc5 [DOC] Use RDoc link style for links to other classes/modules
I used this regex:

([A-Za-z]+)\.html#(?:class|module)-[A-Za-z]+-label-([A-Za-z0-9\-\+]+)

And performed a global find & replace for this:

rdoc-ref:$1@$2
2022-02-07 09:52:06 -05:00
Burdette Lamar 28fb6d6b9e
Adding links to literals and Kernel (#5192)
* Adding links to literals and Kernel
2021-12-03 07:12:28 -06:00
S.H 75aae66c4f
Some codes replace to `RBOOL` macro (#5023)
* Some code replace and using RBOOL macro

* Fix indent

* Using RBOOL in syserr_eqq function
2021-11-09 17:09:29 +09:00
Nobuyoshi Nakada 4fb71575e2
[DOC] Fix code markup [ci skip]
Code markup in RDoc must not be concatenated with anothr word.
2021-10-25 01:04:51 +09:00
Nobuyoshi Nakada e2017f8c7c
Unify iteration arguments 2021-10-10 11:45:50 +09:00
Nobuyoshi Nakada 2293547d9b
Update iteration step in step_i_iter 2021-10-10 11:41:26 +09:00
S.H afb95d1004
Refactor sym_step_i function 2021-10-10 11:40:04 +09:00