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

Create a Chunk from an IterableOnce #2722

Merged
merged 4 commits into from
Nov 29, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/shared/src/main/scala-2.12/fs2/ChunkPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ package fs2
import scala.collection.mutable.{ArrayBuilder, WrappedArray}
import scala.reflect.ClassTag

private[fs2] trait ChunkPlatform[+O] { self: Chunk[O] =>
protected def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make()(ct)
}
private[fs2] trait ChunkPlatform[+O] { self: Chunk[O] => }

private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>

Expand All @@ -37,6 +34,9 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
case _ => None
}

private[fs2] def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make()(ct)

/** Creates a chunk backed by a `WrappedArray`
*/
def wrappedArray[O](wrappedArray: WrappedArray[O]): Chunk[O] = {
Expand Down
10 changes: 7 additions & 3 deletions core/shared/src/main/scala-2.13/fs2/ChunkPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import scala.reflect.ClassTag

private[fs2] trait ChunkPlatform[+O] { self: Chunk[O] =>

protected def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make(ct)

def toArraySeq[O2 >: O: ClassTag]: ArraySeq[O2] = {
val array: Array[O2] = new Array[O2](size)
copyToArray(array)
Expand All @@ -57,10 +54,17 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
case _ => None
}

private[fs2] def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make(ct)

/** Creates a chunk backed by an immutable `ArraySeq`.
*/
def arraySeq[O](arraySeq: immutable.ArraySeq[O]): Chunk[O] = {
val arr = arraySeq.unsafeArray.asInstanceOf[Array[O]]
array(arr)(ClassTag[O](arr.getClass.getComponentType))
}

/** Creates a chunk from a `scala.collection.IterableOnce`. */
def iterableOnce[O](i: collection.IterableOnce[O]): Chunk[O] =
iterator(i.iterator)
}
10 changes: 7 additions & 3 deletions core/shared/src/main/scala-3/fs2/ChunkPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import scala.reflect.ClassTag

private[fs2] trait ChunkPlatform[+O] { self: Chunk[O] =>

protected def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make(ct)

def toArraySeq[O2 >: O: ClassTag]: ArraySeq[O2] = {
val array: Array[O2] = new Array[O2](size)
copyToArray(array)
Expand Down Expand Up @@ -66,6 +63,9 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
case _ => None
}

private[fs2] def makeArrayBuilder[A](implicit ct: ClassTag[A]): ArrayBuilder[A] =
ArrayBuilder.make(ct)

/** Creates a chunk backed by an immutable `ArraySeq`.
*/
def arraySeq[O](arraySeq: immutable.ArraySeq[O]): Chunk[O] = {
Expand Down Expand Up @@ -119,4 +119,8 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
if (offset == 0 && length == values.length) values
else super.toIArray[O2]
}

/** Creates a chunk from a `scala.collection.IterableOnce`. */
def iterableOnce[O](i: collection.IterableOnce[O]): Chunk[O] =
iterator(i.iterator)
}
41 changes: 18 additions & 23 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ abstract class Chunk[+O] extends Serializable with ChunkPlatform[O] with ChunkRu

/** Returns a chunk that has only the elements that satisfy the supplied predicate. */
def filter(p: O => Boolean): Chunk[O] = {
val b = makeArrayBuilder(thisClassTag)
val b = Chunk.makeArrayBuilder(thisClassTag)
b.sizeHint(size)
foreach(e => if (p(e)) b += e)
Chunk.array(b.result()).asInstanceOf[Chunk[O]]
Expand Down Expand Up @@ -209,7 +209,7 @@ abstract class Chunk[+O] extends Serializable with ChunkPlatform[O] with ChunkRu

/** Maps the supplied function over each element and returns a chunk of just the defined results. */
def mapFilter[O2](f: O => Option[O2]): Chunk[O2] = {
val b = makeArrayBuilder[Any]
val b = Chunk.makeArrayBuilder[Any]
b.sizeHint(size)
foreach { o =>
val o2 = f(o)
Expand Down Expand Up @@ -324,7 +324,7 @@ abstract class Chunk[+O] extends Serializable with ChunkPlatform[O] with ChunkRu
*/
def toIndexedChunk: Chunk[O] = this match {
case _: Chunk.Queue[_] =>
val b = makeArrayBuilder[Any]
val b = Chunk.makeArrayBuilder[Any]
b.sizeHint(size)
foreach(o => b += o)
Chunk.array(b.result()).asInstanceOf[Chunk[O]]
Expand Down Expand Up @@ -599,18 +599,22 @@ object Chunk
case ix: GIndexedSeq[O] => indexedSeq(ix)
case _ =>
if (i.isEmpty) empty
Copy link
Contributor

@nikiforo nikiforo Nov 26, 2021

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 and L626?

Copy link
Contributor

@nikiforo nikiforo Nov 29, 2021

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.

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 = Chunk.makeArrayBuilder[Any]
bldr += head
bldr ++= itr
array(bldr.result()).asInstanceOf[Chunk[O]]
} else singleton(head)
}

/** Creates a chunk backed by a mutable `ArraySeq`.
*/
def arraySeq[O](arraySeq: mutable.ArraySeq[O]): Chunk[O] = {
Expand All @@ -621,16 +625,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.
Expand Down