Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
451574f1
Commit
451574f1
authored
Jul 28, 2017
by
Christopher Aue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored Array#bsearch
parent
8a6ce74b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
14 deletions
+25
-14
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+20
-14
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+5
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
451574f1
...
...
@@ -596,30 +596,36 @@ class Array
return
to_enum
:bsearch
unless
block_given?
low
=
0
high
=
s
elf
.
s
ize
high
=
size
satisfied
=
false
while
low
<
high
mid
=
low
+
((
high
-
low
)
/
2
).
truncate
mid
=
((
low
+
high
)
/
2
).
truncate
val
=
self
[
mid
]
v
=
block
.
call
(
val
)
if
v
.
is_a?
(
Integer
)
return
val
if
v
==
0
smaller
=
v
<
0
elsif
v
==
true
res
=
block
.
call
val
case
res
when
0
# find-any mode: Found!
return
val
when
Numeric
# find-any mode: Continue...
in_lower_half
=
res
<
0
when
true
# find-min mode
in_lower_half
=
true
satisfied
=
true
smaller
=
true
elsif
v
==
false
||
v
.
nil?
smaller
=
false
when
false
,
nil
# find-min mode
in_lower_half
=
false
else
raise
TypeError
,
'invalid block result (must be numeric, true, false or nil)'
end
if
smaller
if
in_lower_half
high
=
mid
else
low
=
mid
+
1
end
end
return
nil
if
low
==
self
.
size
return
nil
unless
satisfied
self
[
low
]
satisfied
?
self
[
low
]
:
nil
end
##
...
...
mrbgems/mruby-array-ext/test/array.rb
View file @
451574f1
...
...
@@ -260,6 +260,11 @@ assert("Array#bsearch") do
assert_equal
4
,
a
.
bsearch
{
|
x
|
between
(
0
,
x
,
4
)
}
assert_equal
4
,
a
.
bsearch
{
|
x
|
between
(
4
,
x
,
8
)
}
assert_equal
8
,
a
.
bsearch
{
|
x
|
between
(
5
,
x
,
8
)
}
# Invalid block result
assert_raise
TypeError
,
'invalid block result (must be numeric, true, false or nil)'
do
a
.
bsearch
{
'I like to watch the world burn'
}
end
end
assert
(
"Array#delete_if"
)
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment