final class Buffer[A] extends Serializable
Buffer is a mutable, indexed sequence of values.
Buffer wraps an underlying array, which provides constant-time lookups, updates, and length checks. Values can be appended to or popped from the end of the buffer in amortized constant time. Other operations, such as insert, prepend, and will be linear.
In cases where the type A is known (or the caller is specialized on A), Buffer[A] will store the values in an unboxed array, and will not box values on access or update. To aid in specialization and to avoid inheriting bogus methods, Buffer intentionally does not implement any of Scala's collection traits.
For interop purposes, the toIterable method wraps a buffer in a collections-compatible Iterable[A]. Buffer's iterator method returns an Iterator[A], and the conversion methods .toArray, toVector, and toList are also available.
To facilitate inlining, Buffer's internals are public. However, you should refrain from accessing or modifying these values unless you know what you are doing.
Furthermore, since Buffer is really only useful in cases where you care about space efficiency and performance, Buffer declines to do error-checking above what is provided by the underlying array, or which is necessary to avoid corruption. This means that if you try to access an element beyond the Buffer's length, you are not guaranteed to get an exception (although you will in many cases). This is by design. However, calls which modify the buffer using an invalid index are guaranteed not to corrupt the buffer.
Finally, there is no attempt made to provide any kind of thread safety or protection against concurrent updates. Modify a Buffer during foreach, map, iterator, etc will produce undefined results.
- Self Type
- Buffer[A]
- Alphabetic
- By Inheritance
- Buffer
- Serializable
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Buffer(arr: Array[A], n: Int)(implicit ct: ClassTag[A])
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
def
++(buf: Buffer[A]): Buffer[A]
Concatenate two buffers, returning a new buffer.
Concatenate two buffers, returning a new buffer.
This method does not modify either input buffer, but allocates and returns a new one.
This is an O(n+m) operation, where n and m are the lengths of the input buffers.
-
def
++=(items: Iterable[A]): Unit
Append the values in elems to the end of the buffer.
Append the values in elems to the end of the buffer.
This method is an O(m) operation, where m is the length of items.
-
def
++=(buf: Buffer[A]): Unit
Append the values in buf to the end of the buffer.
Append the values in buf to the end of the buffer.
This method is an O(m) operation, where m is the length of buf.
-
def
++=(arr: Array[A]): Unit
Append the values in arr to the end of the buffer.
Append the values in arr to the end of the buffer.
This method is an O(m) operation, where m is the length of arr.
-
def
+=(a: A): Unit
Append a new value to the end of the buffer.
Append a new value to the end of the buffer.
If there is no space left in the underlying array this method will trigger a grow, increasing the underlying storage capacity.
This is an amortized O(1) operation.
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
append(a: A): Unit
This method is a synonym for append.
-
def
apply(i: Int): A
Return the value at element i.
Return the value at element i.
As noted above, this method may throw an ArrayIndexOutOfBoundsException if i is too large. However, if i is larger than the buffer's length, but fits in the underlying array, a garbage value will be returned instead. Be careful!
This is an O(1) operation.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clear(): Unit1[A]
Clears the buffer's internal state.
Clears the buffer's internal state.
After calling this method, the buffer's state is identical to that obtained by calling Buffer.empty[A].
The previous array is not retained, and will become available for garbage collection.
This is an O(1) operation.
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
compact(): Unit1[A]
Compacts the buffer's internal array to remove extra free space.
Compacts the buffer's internal array to remove extra free space.
This operation should be used it a buffer is not likely to grow again, and the user wants to free any additional memory that may be available.
In general, a buffer that has only grown will use 1-2x of its apparent size. Buffers that have been reduced in size may be using up to 4x the apparent size.
-
def
concat(buf: Buffer[A]): Buffer[A]
This is a synonym for ++.
-
final
def
copy(): Buffer[A]
Copy the buffer's contents to a new buffer.
- implicit val ct: ClassTag[A]
- var elems: Array[A]
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(that: Any): Boolean
Check if two Buffers are equal.
Check if two Buffers are equal.
Equal means the buffers have the same type (which is checked using the ClassTag instances) and the same contents.
Comparing Buffers with any of Scala's collection types will return false.
- Definition Classes
- Buffer → AnyRef → Any
-
def
extend(items: Iterable[A]): Unit
This is a synonym for extend.
-
def
extend(buf: Buffer[A]): Unit
This is a synonym for extend.
-
def
extend(arr: Array[A]): Unit
This is a synonym for ++=.
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
foreach(f: Function[A, Unit]): Unit
Loop over the buffer's contents, appying f to each element.
Loop over the buffer's contents, appying f to each element.
This is an O(n) operation, where n is the length of the buffer.
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
Hash the contents of the buffer to an Int value.
Hash the contents of the buffer to an Int value.
- Definition Classes
- Buffer → AnyRef → Any
-
def
insert(i: Int, a: A): Unit
Insert a new value at index i.
Insert a new value at index i.
For i values that are negative, or greater than the length of the buffer, an exception will be thrown. If i == buffer.length, the value will be appended to the end. Otherwise, this method will shift the values at i and beyond forward to make room.
This is an O(n) operation, where n is buffer.length.
-
def
isEmpty: Boolean
Return true if the Buffer is empty, false otherwise.
Return true if the Buffer is empty, false otherwise.
This is an O(1) operation.
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
iterator(): Iterator[A]
Return an iterator over this buffer's contents.
Return an iterator over this buffer's contents.
This method does not do any copying or locking. Thus, if the buffer is modified while the iterator is "live" the results will be undefined and probably bad.
Use this.copy.iterator to get a "clean" iterator if needed.
Creating the iterator is an O(1) operation.
- var len: Int
-
def
length: Int
Return the length of this Buffer as an Int.
Return the length of this Buffer as an Int.
Since Buffers wrap arrays, their size is limited to what a 32-bit signed integer can represent. In general Buffer should only be used for sequences that are small enough to easily fit into contiguous memory--larger sequences would benefit from block layouts, serialization to/from disk, and other strategies that Buffer does not provide.
This is an O(1) operation.
-
def
map[B](f: (A) ⇒ B)(implicit arg0: ClassTag[B]): Buffer[B]
Map this buffer's contents into a new buffer using f.
Map this buffer's contents into a new buffer using f.
This is an O(n) operation, where n is the length of the buffer.
-
def
max(implicit o: Order[A]): A
Find the maximum value in this buffer.
Find the maximum value in this buffer.
This method uses an instance of Spire's Order[A] type class to compare the elements, to avoid boxing. If you want to use Scala's Ordering, you can use compatibility code in Spire, or call toIterable.min.
-
def
mean(implicit ev: Field[A]): A
Find the mean (average) value of this buffer's contents.
-
def
min(implicit o: Order[A]): A
Find the minimum value in this buffer.
Find the minimum value in this buffer.
This method uses an instance of Spire's Order[A] type class to compare the elements, to avoid boxing. If you want to use Scala's Ordering, you can use compatibility code in Spire, or call toIterable.min.
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
nonEmpty: Boolean
Return true if the Buffer is non-empty, false otherwise.
Return true if the Buffer is non-empty, false otherwise.
This is an O(1) operation.
-
def
norm(p: Int)(implicit ev: Field[A], s: Signed[A], nr: NRoot[A]): A
Find the p-norm of the buffer's contents.
Find the p-norm of the buffer's contents.
The p-norm generalizes notion of a length function.
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
pop(): A
Remove the last element, returning the value returned.
Remove the last element, returning the value returned.
If the buffer is empty, this method throws an exception.
This method is an O(1) operation.
-
def
prepend(a: A): Unit
Insert a new value at the beginning of the buffer.
Insert a new value at the beginning of the buffer.
This method will shift the contents of the buffer forward to make space for the new value.
This is an O(n) operation, where n is buffer.length.
-
def
prependAll(buf: Buffer[A]): Unit
Prepend the values from arr into the beginning of the buffer.
Prepend the values from arr into the beginning of the buffer.
Like splice, this method will shift all the buffer's values back to make room.
This method is an O(m+n) operation, where m is the length of arr, and n is the lenght of the buffer.
-
def
prependAll(arr: Array[A]): Unit
Prepend the values from arr into the beginning of the buffer.
Prepend the values from arr into the beginning of the buffer.
Like splice, this method will shift all the buffer's values back to make room.
This method is an O(m+n) operation, where m is the length of arr, and n is the lenght of the buffer.
-
def
product(implicit ev: MultiplicativeMonoid[A]): A
Multiply the buffer contents together, returning their product.
-
def
remove(i: Int): A
Remove the element at i, returning the value removed.
Remove the element at i, returning the value removed.
This method verifies that the index i is valid; if not, it will throw an exception.
This method is an O(n) operation, where n is buffer.length. Removing the last element of the buffer is O(1) operation, and can also be accomplished with pop.
-
def
reverse(): Buffer[A]
Return a new buffer with this buffer's elements in reverse order.
Return a new buffer with this buffer's elements in reverse order.
This is an O(n) method, where n is buffer.length.
-
def
slice(i: Int, j: Int): Buffer[A]
Return a new buffer which consists of the elements [i, j).
Return a new buffer which consists of the elements [i, j).
The slice is half-open: the resulting buffer will include element i but not element j. In other words, the new buffer will have length (j - i).
If i and j are not valid indices in the buffer, or if i > j, this method will throw an exception.
This is an O(j - i) operation.
-
def
sort(implicit o: Order[A]): Unit
Sort the contents of the buffer.
Sort the contents of the buffer.
This method uses an instance of Spire's Order[A] type class to compare the elements, to avoid boxing. If you want to use Scala's Ordering, you can use compatibility code in Spire, or call toIterable.min.
-
def
splice(i: Int, buf: Buffer[A]): Unit
Splice the values in buf into the buffer at index i.
Splice the values in buf into the buffer at index i.
If i is negative or greater than buffer.length, an exception will be thrown. If i is equal to buffer.length, the buffer will be extended with buf. Otherwise, this method will shift the elements at i and beyond forward to make room for buf's elements. Thus, the size of the buffer will increase by buf.length.
This method is an O(m+n) operation, where m is the length of buf, and n is the length of the buffer.
-
def
splice(i: Int, arr: Array[A]): Unit
Splice the values in arr into the buffer at index i.
Splice the values in arr into the buffer at index i.
If i is negative or greater than buffer.length, an exception will be thrown. If i is equal to buffer.length, the buffer will be extended with arr. Otherwise, this method will shift the elements at i and beyond forward to make room for arr's elements. Thus, the size of the buffer will increase by arr.length.
This method is an O(m+n) operation, where m is the length of arr, and n is the length of the buffer.
-
def
sum(implicit ev: AdditiveMonoid[A]): A
Add the buffer contents together, returning their sum.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toArray(): Array[A]
Create an array out of the elements in the buffer.
Create an array out of the elements in the buffer.
This is an O(n) operation, where n is the length of the buffer.
-
def
toIterable(): Iterable[A]
Wrap this buffer in an Iterable[A] instance.
Wrap this buffer in an Iterable[A] instance.
This method exists as a cheap way to get compatibility with Scala collections without copying/conversion. Note that since Scala collections are not specialized, using this iterable will box values as they are accessed (although the underlying array will still be unboxed).
Like iterator, this method directly wraps the buffer. Thus, you should not mutate the buffer while using the resulting iterable, or risk corruption and undefined behavior.
To get a "safe" value that is compatible with Scala collections, consider using toVector, toList, or copy.toIterable.
Creating the Iterable[A] instance is an O(1) operation.
-
def
toList(): List[A]
Create a List[A] from this buffer's elements.
Create a List[A] from this buffer's elements.
This is an O(n) operation.
-
def
toString(): String
Return a string representation of the contents of the buffer.
Return a string representation of the contents of the buffer.
- Definition Classes
- Buffer → AnyRef → Any
-
def
toVector(): Vector[A]
Create a Vector[A] from this buffer's elements.
Create a Vector[A] from this buffer's elements.
This is an O(n) operation.
-
def
update(i: Int, a: A): Unit
Update the value of element i.
Update the value of element i.
This method has similar caveats to apply. If an illegal i value is used, an ArrayIndexOutOfBoundsException may be thrown. If no exception is thrown, the update will have been ignored. Under no circumstances will an invalid index corrupt the buffer.
This is an O(1) operation.
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )