Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floor for NewVectorIterator.slice limit #10518

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/library/scala/collection/immutable/Vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2389,12 +2389,14 @@ private final class NewVectorIterator[A](v: Vector[A], private[this] var totalLe
}

override def slice(from: Int, until: Int): Iterator[A] = {
val _until =
val _until = mmax(until, 0)

val n =
if(from > 0) {
drop(from)
until - from
Copy link
Contributor

@He-Pin He-Pin Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the most quick fix can be mmax(util, 0), inline it here, otherwise lgtm.

} else until
take(_until)
_until - from
} else _until
take(n)
Copy link
Contributor

@He-Pin He-Pin Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (until<= 0 || until<= from)  Iterator.empty else {
 if (from > 0) drop(from)
 take(if(from < 0) until else until- from)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about your comment. Is this a third proposal? If so, I think it should be until instead of util, and there are some formatting and correctness issues (for instance, .slice(0,0) returns the entire vector, so this would need to be e.g. Iterator.empty).

Copy link
Contributor

@He-Pin He-Pin Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ansvonwa yes, I edit it on my phone, thanks for point out, but I think you can update your pr to avoid the usage of max. and @Ichoran's proposal is very direct too.

}

override def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int = {
Expand Down
6 changes: 6 additions & 0 deletions test/junit/scala/collection/immutable/VectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ class VectorTest {
assertTrue("slice max to min should be empty", Vector(42).slice(Int.MaxValue, Int.MinValue).isEmpty)
}

@Test def `test Vector#iterator slice to MinValue`: Unit = {
assertTrue(Vector(1, 2).iterator.slice(1, Int.MinValue).isEmpty)
assertTrue("slice almost max to min should be empty", Vector(1, 2).iterator.slice(Int.MaxValue - 1, Int.MinValue).isEmpty)
assertTrue("slice max to min should be empty", Vector(1, 2).iterator.slice(Int.MaxValue, Int.MinValue).isEmpty)
}

@Test
def testTail(): Unit = for(size <- verySmallSizes) {
var i = 0
Expand Down