Chunk

abstract class Chunk[+O] extends Serializable with ChunkPlatform[O] with ChunkRuntimePlatform[O]

Immutable, strict, finite sequence of values that supports efficient index-based random access of elements, is memory efficient for all sizes, and avoids unnecessary copying.

Chunks can be created from a variety of collection types using methods on the Chunk companion (e.g., Chunk.array, Chunk.seq, Chunk.vector).

Chunks can be appended via the ++ method. The returned chunk is a composite of the input chunks -- that is, there's no copying of the source chunks. For example, Chunk(1, 2) ++ Chunk(3, 4) ++ Chunk(5, 6) returns a Chunk.Queue(Chunk(1, 2), Chunk(3, 4), Chunk(5, 6)). As a result, indexed based lookup of an appended chunk is amortized O(log2(number of underlying chunks)). In the worst case, where each constituent chunk has size 1, indexed lookup is O(log2(size)). To restore O(1) lookup, call compact, which copies all the underlying chunk elements to a single array backed chunk. Note compact requires a ClassTag of the element type.

Alternatively, a collection of chunks can be directly copied to a new array backed chunk via Chunk.concat(chunks). Like compact, Chunk.concat requires a ClassTag for the element type.

Various subtypes of Chunk are exposed for efficiency reasons:

  • Chunk.Singleton
  • Chunk.ArraySlice
  • Chunk.Queue

In particular, calling .toArraySlice on a chunk returns a Chunk.ArraySlice, which provides access to the underlying backing array, along with an offset and length, referring to a slice of that array.

Companion:
object
trait ChunkPlatform[O]
trait Serializable
class Object
trait Matchable
class Any
class ArraySlice[O]
class Buffer[A, B, C]
class IArraySlice[O]
class Queue[O]
class Singleton[O]

Value members

Abstract methods

def apply(i: Int): O

Returns the element at the specified index. Throws if index is < 0 or >= size.

Returns the element at the specified index. Throws if index is < 0 or >= size.

def copyToArray[O2 >: O](xs: Array[O2], start: Int): Unit

Copies the elements of this chunk in to the specified array at the specified start index.

Copies the elements of this chunk in to the specified array at the specified start index.

def size: Int

Returns the number of elements in this chunk.

Returns the number of elements in this chunk.

Concrete methods

def ++[O2 >: O](that: Chunk[O2]): Chunk[O2]

Returns a chunk which consists of the elements of this chunk and the elements of the supplied chunk. This operation is amortized O(1).

Returns a chunk which consists of the elements of this chunk and the elements of the supplied chunk. This operation is amortized O(1).

def collect[O2](pf: PartialFunction[O, O2]): Chunk[O2]

More efficient version of filter(pf.isDefinedAt).map(pf).

More efficient version of filter(pf.isDefinedAt).map(pf).

def compact[O2 >: O](implicit ct: ClassTag[O2]): ArraySlice[O2]

Converts this chunk to a chunk backed by a single array.

Converts this chunk to a chunk backed by a single array.

Alternatively, call toIndexedChunk to get back a chunk with guaranteed O(1) indexed lookup while also minimizing copying.

def drop(n: Int): Chunk[O]

Drops the first n elements of this chunk.

Drops the first n elements of this chunk.

def dropRight(n: Int): Chunk[O]

Drops the right-most n elements of this chunk queue in a way that preserves chunk structure.

Drops the right-most n elements of this chunk queue in a way that preserves chunk structure.

override def equals(a: Any): Boolean
Definition Classes
Any
def filter(p: O => Boolean): Chunk[O]

Returns a chunk that has only the elements that satisfy the supplied predicate.

Returns a chunk that has only the elements that satisfy the supplied predicate.

def find(p: O => Boolean): Option[O]

Returns the first element for which the predicate returns true or None if no elements satisfy the predicate.

Returns the first element for which the predicate returns true or None if no elements satisfy the predicate.

def flatMap[O2](f: O => Chunk[O2]): Chunk[O2]

Maps f over the elements of this chunk and concatenates the result.

Maps f over the elements of this chunk and concatenates the result.

def foldLeft[A](init: A)(f: (A, O) => A): A

Left-folds the elements of this chunk.

Left-folds the elements of this chunk.

def forall(p: O => Boolean): Boolean

Returns true if the predicate passes for all elements.

Returns true if the predicate passes for all elements.

def foreach(f: O => Unit): Unit

Invokes the supplied function for each element of this chunk.

Invokes the supplied function for each element of this chunk.

def foreachWithIndex(f: (O, Int) => Unit): Unit

Like foreach but includes the index of the element.

Like foreach but includes the index of the element.

override def hashCode: Int
Definition Classes
Any
def head: Option[O]

Gets the first element of this chunk.

Gets the first element of this chunk.

def indexWhere(p: O => Boolean): Option[Int]

Returns the index of the first element which passes the specified predicate (i.e., p(i) == true) or None if no elements pass the predicate.

Returns the index of the first element which passes the specified predicate (i.e., p(i) == true) or None if no elements pass the predicate.

final def isEmpty: Boolean

True if size is zero, false otherwise.

True if size is zero, false otherwise.

Creates an iterator that iterates the elements of this chunk. The returned iterator is not thread safe.

Creates an iterator that iterates the elements of this chunk. The returned iterator is not thread safe.

def last: Option[O]

Gets the last element of this chunk.

Gets the last element of this chunk.

def map[O2](f: O => O2): Chunk[O2]

Creates a new chunk by applying f to each element in this chunk.

Creates a new chunk by applying f to each element in this chunk.

def mapAccumulate[S, O2](init: S)(f: (S, O) => (S, O2)): (S, Chunk[O2])

Maps the supplied stateful function over each element, outputting the final state and the accumulated outputs. The first invocation of f uses init as the input state value. Each successive invocation uses the output state of the previous invocation.

Maps the supplied stateful function over each element, outputting the final state and the accumulated outputs. The first invocation of f uses init as the input state value. Each successive invocation uses the output state of the previous invocation.

def mapFilter[O2](f: O => Option[O2]): Chunk[O2]

Maps the supplied function over each element and returns a chunk of just the defined results.

Maps the supplied function over each element and returns a chunk of just the defined results.

final def nonEmpty: Boolean

False if size is zero, true otherwise.

False if size is zero, true otherwise.

Creates an iterator that iterates the elements of this chunk in reverse order. The returned iterator is not thread safe.

Creates an iterator that iterates the elements of this chunk in reverse order. The returned iterator is not thread safe.

def scanLeft[O2](z: O2)(f: (O2, O) => O2): Chunk[O2]

Like foldLeft but emits each intermediate result of f.

Like foldLeft but emits each intermediate result of f.

def scanLeftCarry[O2](z: O2)(f: (O2, O) => O2): (Chunk[O2], O2)

Like scanLeft except the final element is emitted as a standalone value instead of as the last element of the accumulated chunk.

Like scanLeft except the final element is emitted as a standalone value instead of as the last element of the accumulated chunk.

Equivalent to val b = a.scanLeft(z)(f); val (c, carry) = b.splitAt(b.size - 1).

def splitAt(n: Int): (Chunk[O], Chunk[O])

Splits this chunk in to two chunks at the specified index.

Splits this chunk in to two chunks at the specified index.

def startsWith[O2 >: O](seq: Seq[O2]): Boolean

Check to see if this starts with the items in the given seq.

Check to see if this starts with the items in the given seq.

def take(n: Int): Chunk[O]

Takes the first n elements of this chunk.

Takes the first n elements of this chunk.

def takeRight(n: Int): Chunk[O]

Takes the right-most n elements of this chunk queue in a way that preserves chunk structure.

Takes the right-most n elements of this chunk queue in a way that preserves chunk structure.

def to(collector: Collector[O]): Out

Converts this chunk to a new collection using the supplied collector.

Converts this chunk to a new collection using the supplied collector.

Example:
scala> Chunk(1, 2, 3).to(Set)
def toArray[O2 >: O : ClassTag]: Array[O2]

Copies the elements of this chunk to an array.

Copies the elements of this chunk to an array.

def toArraySlice[O2 >: O](implicit ct: ClassTag[O2]): ArraySlice[O2]

Converts this chunk to a Chunk.ArraySlice.

Converts this chunk to a Chunk.ArraySlice.

def toBitVector[B >: O](implicit ev: B =:= Byte): BitVector

Converts this chunk to a scodec-bits BitVector.

Converts this chunk to a scodec-bits BitVector.

def toByteBuffer[B >: O](implicit ev: B =:= Byte): ByteBuffer

Converts this chunk to a java.nio.ByteBuffer.

Converts this chunk to a java.nio.ByteBuffer.

def toByteVector[B >: O](implicit ev: B =:= Byte): ByteVector

Converts this chunk to a scodec-bits ByteVector.

Converts this chunk to a scodec-bits ByteVector.

def toChain: Chain[O]

Converts this chunk to a chain.

Converts this chunk to a chain.

Returns a chunk with guaranteed O(1) lookup by index.

Returns a chunk with guaranteed O(1) lookup by index.

Unlike compact, this operation does not copy any elements unless this chunk does not provide O(1) lookup by index -- e.g., a chunk built via 1 or more usages of ++.

def toList: List[O]

Converts this chunk to a list.

Converts this chunk to a list.

Converts this chunk to a NonEmptyList

Converts this chunk to a NonEmptyList

override def toString: String
Definition Classes
Any

Converts this chunk to a vector.

Converts this chunk to a vector.

def traverse[F[_], O2](f: O => F[O2])(implicit F: Applicative[F]): F[Chunk[O2]]
def traverseFilter[F[_], O2](f: O => F[Option[O2]])(implicit F: Applicative[F]): F[Chunk[O2]]
def zip[O2](that: Chunk[O2]): Chunk[(O, O2)]

Zips this chunk the the supplied chunk, returning a chunk of tuples.

Zips this chunk the the supplied chunk, returning a chunk of tuples.

def zipWith[O2, O3](that: Chunk[O2])(f: (O, O2) => O3): Chunk[O3]

Zips this chunk with the supplied chunk, passing each pair to f, resulting in an output chunk.

Zips this chunk with the supplied chunk, passing each pair to f, resulting in an output chunk.

Zips the elements of the input chunk with its indices, and returns the new chunk.

Zips the elements of the input chunk with its indices, and returns the new chunk.

Example:
scala> Chunk("The", "quick", "brown", "fox").zipWithIndex.toList
res0: List[(String, Int)] = List((The,0), (quick,1), (brown,2), (fox,3))

Deprecated methods

@deprecated("Unsound when used with primitives, use compactBoxed instead", "3.1.6")
def compactUntagged[O2 >: O]: ArraySlice[O2]

Like compact but does not require a ClassTag. Elements are boxed and stored in an Array[Any].

Like compact but does not require a ClassTag. Elements are boxed and stored in an Array[Any].

Deprecated

Inherited methods

def toArraySeq[O2 >: O : ClassTag]: ArraySeq[O2]
Inherited from:
ChunkPlatform
Inherited from:
ChunkPlatform
def toIArray[O2 >: O : ClassTag]: IArray[O2]
Inherited from:
ChunkPlatform
def toIArraySlice[O2 >: O](implicit ct: ClassTag[O2]): IArraySlice[O2]
Inherited from:
ChunkPlatform