RingBuffer

class RingBuffer<T>(val capacity: Int) : Iterable<T>

A fixed-capacity buffer that overwrites its oldest element when full.

Elements are ordered oldest first. add appends in constant amortized time and, when the buffer already holds capacity elements, drops the oldest one. Iteration and toList yield elements oldest first. Backed by ArrayDeque.

This class is not thread-safe.

Type Parameters

T

the element type.

Throws

Constructors

Link copied to clipboard
constructor(capacity: Int)

Creates an empty buffer that holds at most capacity elements.

Properties

Link copied to clipboard

The maximum number of elements the buffer holds. Always positive.

Link copied to clipboard
val size: Int

The number of elements currently in the buffer, from 0 to capacity.

Functions

Link copied to clipboard
fun add(element: T)

Appends element as the newest element, dropping the oldest element first when the buffer is full.

Link copied to clipboard
fun addAll(elements: Iterable<T>)

Appends every element of elements in iteration order through add.

Link copied to clipboard

Returns true when every element equals the first one. An empty iterable returns true.

Link copied to clipboard

Returns true if at least one element occurs more than once.

Link copied to clipboard

Returns a map of each element to its index in this iterable.

Link copied to clipboard

Returns the average of all durations.

Link copied to clipboard
fun <T, R> Iterable<T>.cartesianProduct(other: Iterable<R>): List<Pair<T, R>>

Returns every pairing of an element of this iterable with an element of other.

Link copied to clipboard
inline fun <T, K> Iterable<T>.chunkedBy(selector: (T) -> K): List<List<T>>

Splits this iterable into consecutive runs, starting a new run whenever the value of selector changes.

Link copied to clipboard
fun clear()

Removes every element from the buffer.

Link copied to clipboard
inline fun <T, K> Iterable<T>.countBy(selector: (T) -> K): Map<K, Int>

Returns a map of each distinct value of selector to the number of elements producing it.

Link copied to clipboard
fun <T> Iterable<T>.duplicates(): Set<T>

Returns the set of elements that occur more than once.

Link copied to clipboard
fun first(): T

Returns the oldest element.

Link copied to clipboard
fun firstOrNull(): T?

Returns the oldest element, or null if the buffer is empty.

Link copied to clipboard

Returns a map of each distinct element to the number of times it occurs.

Link copied to clipboard

Returns true if no element occurs more than once.

Link copied to clipboard

Returns true if the buffer holds no elements.

Link copied to clipboard

Returns true if the buffer holds exactly capacity elements.

Link copied to clipboard
open operator override fun iterator(): Iterator<T>

Returns an iterator over a snapshot of the elements, oldest first. Mutating the buffer after this call does not affect the iterator.

Link copied to clipboard
fun last(): T

Returns the newest element.

Link copied to clipboard
fun lastOrNull(): T?

Returns the newest element, or null if the buffer is empty.

Link copied to clipboard
inline fun <T, R> Iterable<T>.mapToSet(transform: (T) -> R): Set<R>

Returns a set of the results of applying transform to each element.

Link copied to clipboard

Returns the smallest and largest element as a pair, or null if the receiver is empty.

Link copied to clipboard
fun <T> Iterable<T>.mode(): T?

Returns the most frequent element, or null if the receiver is empty.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilter(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the matching elements in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilterNot(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the non-matching elements in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelFlatMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> Iterable<R>): List<R>

Transforms every element concurrently and returns the flattened results in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEach(concurrency: Int = Int.MAX_VALUE, action: suspend (T) -> Unit)

Runs action for every element concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEachIndexed(concurrency: Int = Int.MAX_VALUE, action: suspend (index: Int, element: T) -> Unit)

Runs action for every element with its index concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R): List<R>

Transforms every element concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMapIndexed(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R): List<R>

Transforms every element with its index concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapIndexedNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R?): List<R>

Transforms every element with its index concurrently and returns the non-null results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R?): List<R>

Transforms every element concurrently and returns the non-null results in the original order.

Link copied to clipboard
inline fun <T> Iterable<T>.partitionIndexed(predicate: (index: Int, T) -> Boolean): Pair<List<T>, List<T>>

Splits this iterable into a pair of lists using predicate, which also receives each element's index.

Link copied to clipboard
@JvmName(name = "productOfDouble")
fun Iterable<Double>.product(): Double
@JvmName(name = "productOfLong")
fun Iterable<Long>.product(): Long

Returns the product of all elements.

@JvmName(name = "productOfInt")
fun Iterable<Int>.product(): Long

Returns the product of all elements, accumulated as a Long.

Link copied to clipboard
inline fun <T> Iterable<T>.splitBy(isDelimiter: (T) -> Boolean): List<List<T>>

Splits this iterable into segments separated by elements matching isDelimiter, dropping the delimiters.

Link copied to clipboard

Returns the sum of all durations, or Duration.ZERO for an empty iterable.

Link copied to clipboard
fun toList(): List<T>

Returns a snapshot of the elements, oldest first.

Link copied to clipboard
inline fun <T> Iterable<AutoCloseable>.useAll(block: () -> T): T

Runs block and then closes every resource in this collection in reverse iteration order.