Commit 69b6a9a1
69b6a9a10249Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Implement Array#bsearch on int and string arrays
Binary-search a sorted typed array in find-minimum mode for the first element where the block is truthy, returning that element or nil. int arrays yield a nullable int (SP_INT_NIL), str arrays a string (NULL renders as nil). Other element kinds fall through to the unresolved path.
Refs #1216
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 69b6a9a10249
- Parent
- e26c4419b7fe
spinel_analyze.rbmodifiedView file
@@ -5593,10 +5593,18 @@
55935593
return "tuple:int,int"55945594
end55955595
# Integer Range#bsearch returns a member of the range or nil when5596-
# the block never finds one, so it is a nullable int.5596+
# the block never finds one, so it is a nullable int. Array#bsearch5597+
# returns an element of the (sorted) array or nil: int arrays yield a5598+
# nullable int, str arrays a string (NULL renders as nil).55975599
if mname == "bsearch" && @nd_block[nid] >= 05598-
if recv >= 0 && infer_type(recv) == "range"5599-
return "int?"5600+
if recv >= 05601+
rt_bs = infer_type(recv)5602+
if rt_bs == "range" || rt_bs == "int_array"5603+
return "int?"5604+
end5605+
if rt_bs == "str_array"5606+
return "string"5607+
end56005608
end56015609
end56025610
if mname == "partition"spinel_codegen.rbmodifiedView file
Loading diff…
test/array_bsearch.rbaddedView file
Loading diff…
test/array_bsearch.rb.expectedaddedView file
Loading diff…