Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Conversation

julienrf
Copy link
Contributor

@julienrf julienrf commented Jul 4, 2017

Fixes #131.

Note that I kept the grouped and sliding methods of Iterator even though they also use buffers. Since these methods do exist in 2.12 we have to discuss before we remove them…

@julienrf julienrf requested review from Ichoran and szeiger July 4, 2017 12:03
protected val normN = n max 0
override def knownSize =
if (underlying.knownSize >= 0) (underlying.knownSize - normN) max 0 else -1
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the DropRight view which was based on the dropRight method of Iterator. (same for TakeRight below)

}
}

override def knownSize: Int = length
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s unrelated to the PR, actually, but I thought we could override knownSize in the case of IndexedView. We should do the same for IndexedSeq.

private val delta = underlying.length - normN
def length = underlying.length min normN
def apply(i: Int) = underlying.apply(i)
def apply(i: Int) = underlying.apply(i + delta)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept the IndexedView based implementation of takeRight (and dropRight, below)

val lead = coll.iterator() drop n
val it = coll.iterator()
while (lead.hasNext) {
lead.next()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a cute algorithm, but I can't imagine it's faster than just going

var m = this.size
val it = coll.iterator()
while (m > n) it.next
while (it.hasNext) b += it.next()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would have the benefit of requiring only one traversal, indeed (excepted in the case of linear collections). I should give it a try.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried it but didn’t notice a difference (in List):

takeright

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, what? List shouldn't rebuild on takeRight anyway--if it does, that needs to be fixed. Also, I didn't suggest the other method is faster, just more obviously correct.

Copy link
Contributor Author

@julienrf julienrf Jul 19, 2017

Choose a reason for hiding this comment

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

Indeed good point, that’s anyway something that we should fix… (#162 keeps track of that)

def dropRight(n: Int): C = fromSpecificIterable(View.DropRight(coll, n))
def dropRight(n: Int): C = {
val b = newSpecificBuilder()
if (n >= 0) b.sizeHint(coll, -n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why -n?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This version of sizeHint takes a collection and a delta to apply to the collection’s size. Here we want to say that the size of the result will be the size of coll minus the number of dropped elemnts. I could write explicitly sizeHint(coll, delta = -n), for clarity.

extends View.TakeRight(underlying, n) with IndexedView[A] {
override def iterator() = super.iterator() // needed to avoid "conflicting overrides" error
class TakeRight[A](underlying: IndexedView[A], n: Int) extends IndexedView[A] {
private val normN = n max 0
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to store this value. def length = underlying.length - delta is enough.

extends View.DropRight(underlying, n) with IndexedView[A] {
override def iterator() = super.iterator()
class DropRight[A](underlying: IndexedView[A], n: Int) extends IndexedView[A] {
private val normN = n max 0
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just store the actual length here; you never need n max 0 separately.

@Ichoran
Copy link
Contributor

Ichoran commented Jul 4, 2017

Looks reasonable. I made some comments but didn't do a formal review.

@julienrf julienrf merged commit 1505027 into master Jul 20, 2017
@julienrf julienrf deleted the iterator-buffering branch July 20, 2017 07:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants