OrderedScatterSet


Known direct subclasses
MutableOrderedScatterSet

MutableOrderedScatterSet is a container with a MutableSet-like interface based on a flat hash table implementation that preserves the insertion order for iteration.


OrderedScatterSet is a container with a Set-like interface based on a flat hash table implementation that preserves the insertion order for iteration. The underlying implementation is designed to avoid all allocations on insertion, removal, retrieval, and iteration. Allocations may still happen on insertion when the underlying storage needs to grow to accommodate newly added elements to the set.

This implementation guarantees the order of the elements when iterating over them using forEach or the iterator provided by asSet.

Though OrderedScatterSet offers a read-only interface, it is always backed by a MutableOrderedScatterSet. Read operations alone are thread-safe. However, any mutations done through the backing MutableScatterSet while reading on another thread are not safe and the developer must protect the set from such changes during read operations.

Note: when a Set is absolutely necessary, you can use the method asSet to create a thin wrapper around an OrderedScatterSet. Please refer to asSet for more details and caveats.

Summary

Protected constructors

Cmn

Public functions

inline Boolean
all(predicate: (element) -> Boolean)

Returns true if all elements match the given predicate.

Cmn
Boolean
any()

Returns true if this set has at least one element.

Cmn
inline Boolean
any(predicate: (element) -> Boolean)

Returns true if at least one element matches the given predicate.

Cmn
Set<E>

Wraps this OrderedScatterSet with a Set interface.

Cmn
operator Boolean
contains(element: E)

Returns true if the specified element is present in this hash set, false otherwise.

Cmn
@IntRange(from = 0) Int

Returns the number of elements in this set.

Cmn
inline @IntRange(from = 0) Int
count(predicate: (element) -> Boolean)

Returns the number of elements matching the given predicate.The order in which the predicate is called over the elements is arbitrary.

Cmn
open operator Boolean
equals(other: Any?)

Compares the specified object other with this hash set for equality.

Cmn
E

Returns the first element in the collection.

Cmn
inline E
first(predicate: (element) -> Boolean)

Returns the first element in insertion order in the collection for which predicate returns true.

Cmn
inline E?
firstOrNull(predicate: (element) -> Boolean)

Returns the first element in insertion order in the collection for which predicate returns true or null if there are no elements that match predicate.

Cmn
inline Unit
forEach(block: (element) -> Unit)

Iterates over every element stored in this set by invoking the specified block lambda.

Cmn
inline Unit
forEachReverse(block: (element) -> Unit)

Iterates over every element stored in this set by invoking the specified block lambda.

Cmn
open Int

Returns the hash code value for this set.

Cmn
Boolean

Indicates whether this set is empty.

Cmn
Boolean

Returns true if this set is not empty.

Cmn
String
joinToString(
    separator: CharSequence,
    prefix: CharSequence,
    postfix: CharSequence,
    limit: Int,
    truncated: CharSequence,
    transform: ((E) -> CharSequence)?
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

Cmn
E

Returns the last element in the collection.

Cmn
inline E
last(predicate: (element) -> Boolean)

Returns the last element in insertion order in the collection for which predicate returns true.

Cmn
inline E?
lastOrNull(predicate: (element) -> Boolean)

Returns the first element in insertion order in the collection for which predicate returns true or null if there are no elements that match predicate.

Cmn
Boolean

Returns true if this set has no elements.

Cmn
inline List<E>

Returns a new list containing this set's entries, in insertion order.

Cmn
open String

Returns a string representation of this set.

Cmn

Public properties

Int

Returns the number of elements that can be stored in this set without requiring internal storage reallocation.

Cmn
Int

Returns the number of elements in this set.

Cmn

Protected constructors

OrderedScatterSet

protected <E : Any?> OrderedScatterSet()

Public functions

all

inline fun all(predicate: (element) -> Boolean): Boolean

Returns true if all elements match the given predicate. If there are no elements in the set, true is returned. The order in which the predicate is called over the elements is arbitrary.

Parameters
predicate: (element) -> Boolean

called for elements in the set to determine if it returns return true for all elements.

any

fun any(): Boolean

Returns true if this set has at least one element.

any

inline fun any(predicate: (element) -> Boolean): Boolean

Returns true if at least one element matches the given predicate. The order in which the predicate is called over the elements is arbitrary.

Parameters
predicate: (element) -> Boolean

called for elements in the set to determine if it returns true for any elements.

asSet

fun asSet(): Set<E>

Wraps this OrderedScatterSet with a Set interface. The Set is backed by the OrderedScatterSet, so changes to the OrderedScatterSet are reflected in the Set. If the OrderedScatterSet is modified while an iteration over the Set is in progress, the results of the iteration are undefined.

Note: while this method is useful to use this OrderedScatterSet with APIs accepting Set interfaces, it is less efficient to do so than to use OrderedScatterSet's APIs directly. While the Set implementation returned by this method tries to be as efficient as possible, the semantics of Set may require the allocation of temporary objects for access and iteration.

contains

operator fun contains(element: E): Boolean

Returns true if the specified element is present in this hash set, false otherwise.

Parameters
element: E

The element to look for in this set

count

fun count(): @IntRange(from = 0) Int

Returns the number of elements in this set.

count

inline fun count(predicate: (element) -> Boolean): @IntRange(from = 0) Int

Returns the number of elements matching the given predicate.The order in which the predicate is called over the elements is arbitrary.

Parameters
predicate: (element) -> Boolean

Called for all elements in the set to count the number for which it returns true.

equals

open operator fun equals(other: Any?): Boolean

Compares the specified object other with this hash set for equality. The two objects are considered equal if other:

first

fun first(): E

Returns the first element in the collection.

Throws
kotlin.NoSuchElementException

if the collection is empty

first

inline fun first(predicate: (element) -> Boolean): E

Returns the first element in insertion order in the collection for which predicate returns true.

Parameters
predicate: (element) -> Boolean

called with each element until it returns true.

Returns
E

The element for which predicate returns true.

Throws
kotlin.NoSuchElementException

if predicate returns false for all elements or the collection is empty.

firstOrNull

inline fun firstOrNull(predicate: (element) -> Boolean): E?

Returns the first element in insertion order in the collection for which predicate returns true or null if there are no elements that match predicate.

Parameters
predicate: (element) -> Boolean

called with each element until it returns true.

Returns
E?

The element for which predicate returns true or null if there are no elements in the set or predicate returned false for every element in the set.

forEach

inline fun forEach(block: (element) -> Unit): Unit

Iterates over every element stored in this set by invoking the specified block lambda. The iteration order is the same as the insertion order. It is safe to remove the element passed to block during iteration.

Parameters
block: (element) -> Unit

called with each element in the set

forEachReverse

inline fun forEachReverse(block: (element) -> Unit): Unit

Iterates over every element stored in this set by invoking the specified block lambda. The iteration order is the reverse of the insertion order. It is safe to remove the element passed to block during iteration.

Parameters
block: (element) -> Unit

called with each element in the set

hashCode

open fun hashCode(): Int

Returns the hash code value for this set. The hash code of a set is based on the sum of the hash codes of the elements in the set, where the hash code of a null element is defined to be zero.

isEmpty

fun isEmpty(): Boolean

Indicates whether this set is empty.

isNotEmpty

fun isNotEmpty(): Boolean

Returns true if this set is not empty.

joinToString

fun joinToString(
    separator: CharSequence = ", ",
    prefix: CharSequence = "",
    postfix: CharSequence = "",
    limit: Int = -1,
    truncated: CharSequence = "...",
    transform: ((E) -> CharSequence)? = null
): String

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

When a non-negative value of limit is provided, a maximum of limit items are used to generate the string. If the collection holds more than limit items, the string is terminated with truncated.

transform may be supplied to convert each element to a custom String.

last

fun last(): E

Returns the last element in the collection.

Throws
kotlin.NoSuchElementException

if the collection is empty

last

inline fun last(predicate: (element) -> Boolean): E

Returns the last element in insertion order in the collection for which predicate returns true.

Parameters
predicate: (element) -> Boolean

called with each element until it returns true.

Returns
E

The element for which predicate returns true.

Throws
kotlin.NoSuchElementException

if predicate returns false for all elements or the collection is empty.

lastOrNull

inline fun lastOrNull(predicate: (element) -> Boolean): E?

Returns the first element in insertion order in the collection for which predicate returns true or null if there are no elements that match predicate.

Parameters
predicate: (element) -> Boolean

called with each element until it returns true.

Returns
E?

The element for which predicate returns true or null if there are no elements in the set or predicate returned false for every element in the set.

none

fun none(): Boolean

Returns true if this set has no elements.

toList

inline fun toList(): List<E>

Returns a new list containing this set's entries, in insertion order.

toString

open fun toString(): String

Returns a string representation of this set. The set is denoted in the string by the []. Each element is separated by , .

Public properties

capacity

val capacityInt

Returns the number of elements that can be stored in this set without requiring internal storage reallocation.

size

val sizeInt

Returns the number of elements in this set.