-
Notifications
You must be signed in to change notification settings - Fork 603
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
Create a Chunk from an IterableOnce #2722
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -596,18 +596,22 @@ object Chunk | |
case ix: GIndexedSeq[O] => indexedSeq(ix) | ||
case _ => | ||
if (i.isEmpty) empty | ||
else { | ||
val itr = i.iterator | ||
val head = itr.next() | ||
if (itr.hasNext) { | ||
val bldr = collection.mutable.Buffer.newBuilder[O] | ||
bldr += head | ||
bldr ++= itr | ||
buffer(bldr.result()) | ||
} else singleton(head) | ||
} | ||
else iterator(i.iterator) | ||
}) | ||
|
||
/** Creates a chunk from a `scala.collection.Iterator`. */ | ||
def iterator[O](itr: collection.Iterator[O]): Chunk[O] = | ||
if (itr.isEmpty) empty | ||
else { | ||
val head = itr.next() | ||
if (itr.hasNext) { | ||
val bldr = collection.mutable.Buffer.newBuilder[O] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In those cases we have the (expected) size, we don't in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ArrayBuilder should still beat mutable buffer though, even without a size hint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using That would mean that we couldn't use it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge the main branch in to this PR and then use |
||
bldr += head | ||
bldr ++= itr | ||
buffer(bldr.result()) | ||
} else singleton(head) | ||
} | ||
|
||
/** Creates a chunk backed by a mutable `ArraySeq`. | ||
*/ | ||
def arraySeq[O](arraySeq: mutable.ArraySeq[O]): Chunk[O] = { | ||
|
@@ -618,16 +622,7 @@ object Chunk | |
/** Creates a chunk backed by a `Chain`. */ | ||
def chain[O](c: Chain[O]): Chunk[O] = | ||
if (c.isEmpty) empty | ||
else { | ||
val itr = c.iterator | ||
val head = itr.next() | ||
if (itr.hasNext) { | ||
val bldr = collection.mutable.Buffer.newBuilder[O] | ||
bldr += head | ||
bldr ++= itr | ||
buffer(bldr.result()) | ||
} else singleton(head) | ||
} | ||
else iterator(c.iterator) | ||
|
||
/** Creates a chunk backed by a mutable buffer. The underlying buffer must not be modified after | ||
* it is passed to this function. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emptiness gets checked in
iterableOnce
, can we remove this check from here andL626
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the implementation has changed and it's an optimization. Thus, the comment is resolved.