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
a3bfd735
Commit
a3bfd735
authored
Jul 28, 2017
by
Christopher Aue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Array#bsearch_index
parent
451574f1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
4 deletions
+29
-4
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+25
-4
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+4
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
a3bfd735
...
...
@@ -595,18 +595,39 @@ class Array
def
bsearch
(
&
block
)
return
to_enum
:bsearch
unless
block_given?
if
idx
=
bsearch_index
(
&
block
)
self
[
idx
]
else
nil
end
end
##
# call-seq:
# ary.bsearch_index {|x| block } -> int or nil
#
# By using binary search, finds an index of a value from this array which
# meets the given condition in O(log n) where n is the size of the array.
#
# It supports two modes, depending on the nature of the block and they are
# exactly the same as in the case of #bsearch method with the only difference
# being that this method returns the index of the element instead of the
# element itself. For more details consult the documentation for #bsearch.
def
bsearch_index
(
&
block
)
return
to_enum
:bsearch_index
unless
block_given?
low
=
0
high
=
size
satisfied
=
false
while
low
<
high
mid
=
((
low
+
high
)
/
2
).
truncate
val
=
self
[
mid
]
res
=
block
.
call
val
res
=
block
.
call
self
[
mid
]
case
res
when
0
# find-any mode: Found!
return
val
return
mid
when
Numeric
# find-any mode: Continue...
in_lower_half
=
res
<
0
when
true
# find-min mode
...
...
@@ -625,7 +646,7 @@ class Array
end
end
satisfied
?
self
[
low
]
:
nil
satisfied
?
low
:
nil
end
##
...
...
mrbgems/mruby-array-ext/test/array.rb
View file @
a3bfd735
...
...
@@ -267,6 +267,10 @@ assert("Array#bsearch") do
end
end
assert
(
"Array#bsearch_index"
)
do
# tested through Array#bsearch
end
assert
(
"Array#delete_if"
)
do
a
=
[
1
,
2
,
3
,
4
,
5
]
assert_equal
[
1
,
2
,
3
,
4
,
5
],
a
.
delete_if
{
false
}
...
...
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