Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
accbc5bb
authored
Jan 24, 2020
by
kimvde
Committed by
Andrew Lewis
Jan 27, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add binarySearchFloor() for LongArrays
PiperOrigin-RevId: 291397882
parent
7b05f338
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
library/common/src/main/java/com/google/android/exoplayer2/util/Util.java
library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java
library/common/src/main/java/com/google/android/exoplayer2/util/Util.java
View file @
accbc5bb
...
...
@@ -835,6 +835,47 @@ public final class Util {
}
/**
* Returns the index of the largest element in {@code longArray} that is less than (or optionally
* equal to) a specified {@code value}.
*
* <p>The search is performed using a binary search algorithm, so the array must be sorted. If the
* array contains multiple elements equal to {@code value} and {@code inclusive} is true, the
* index of the first one will be returned.
*
* @param longArray The array to search.
* @param value The value being searched for.
* @param inclusive If the value is present in the array, whether to return the corresponding
* index. If false then the returned index corresponds to the largest element strictly less
* than the value.
* @param stayInBounds If true, then 0 will be returned in the case that the value is smaller than
* the smallest element in the array. If false then -1 will be returned.
* @return The index of the largest element in {@code array} that is less than (or optionally
* equal to) {@code value}.
*/
public
static
int
binarySearchFloor
(
LongArray
longArray
,
long
value
,
boolean
inclusive
,
boolean
stayInBounds
)
{
int
lowIndex
=
0
;
int
highIndex
=
longArray
.
size
()
-
1
;
while
(
lowIndex
<=
highIndex
)
{
int
midIndex
=
(
lowIndex
+
highIndex
)
>>>
1
;
if
(
longArray
.
get
(
midIndex
)
<
value
)
{
lowIndex
=
midIndex
+
1
;
}
else
{
highIndex
=
midIndex
-
1
;
}
}
if
(
inclusive
&&
highIndex
+
1
<
longArray
.
size
()
&&
longArray
.
get
(
highIndex
+
1
)
==
value
)
{
highIndex
++;
}
else
if
(
stayInBounds
&&
highIndex
==
-
1
)
{
highIndex
=
0
;
}
return
highIndex
;
}
/**
* Returns the index of the smallest element in {@code array} that is greater than (or optionally
* equal to) a specified {@code value}.
*
...
...
library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java
View file @
accbc5bb
This diff is collapsed.
Click to expand it.
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