WeightedSortedList

class WeightedSortedList<E> : ObjectList<E> , RandomAccess, Object2DoubleFunction<E>

A list of elements kept sorted by their associated weight (non-decreasing).

This collection stores items and their corresponding weights in parallel arrays. The weights must always satisfy:

  • all weights are within the configured [lowerBound, upperBound] (bounds may be exclusive),

  • the weights array is non-decreasing (monotonic non-decreasing),

  • items.size == weights.size.

The list exposes the usual List-like operations, but enforces weight constraints and preserves sorted order on all mutating operations. Elements are compared by the double weight returned by the supplied weighter function (not by element natural order or comparator).

This class implements Object2DoubleFunction<E> so you can query an element's weight via getDouble(key). A defaultReturnValue is used when a key is not present.

Thread-safety: not thread-safe — external synchronization required for concurrent access.

Typical time complexities (n = size):

  • add(e): O(log n) to locate position + O(n) to insert (worst-case shifting) → O(n)

  • removeAt(index): O(n) to shift elements → O(n)

  • get / index-based reads: O(1)

  • getDouble(key): O(n) (uses indexOf)

Example:

// constructor takes lower/upper bounds and a weighter function:
val list = WeightedSortedList<String>(lowerBound = 0.0, upperBound = 1.0, weighter = { s -> s.length.toDouble() })
list.add("a") // inserted according to weight

Constructors

Link copied to clipboard
constructor(defaultCapacity: Int = 0, lowerBound: Double = Double.NEGATIVE_INFINITY, lowerBoundInclusive: Boolean = true, upperBound: Double = Double.POSITIVE_INFINITY, upperBoundInclusive: Boolean = true, weighter: ToDoubleFunction<in E>)

Convenience constructor that creates empty internal storage with the given default capacity.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override val size: Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
open override fun add(element: E): Boolean

Insert element into the list at the correct sorted position according to its weight.

open override fun add(index: Int, element: E)

Insert element at the specified index while preserving the sorted-by-weight invariant.

Link copied to clipboard
open fun addAll(p0: ObjectList<out E>): Boolean
open fun addAll(p0: Int, p1: ObjectList<out E>): Boolean

open override fun addAll(elements: Collection<E>): Boolean

Add all elements from the given collection while preserving the sorted-by-weight invariant.

open override fun addAll(index: Int, elements: Collection<E>): Boolean

Insert a contiguous block of elements at index.

Link copied to clipboard
open override fun addElements(p0: Int, p1: Array<E>)

open override fun addElements(index: Int, a: Array<out E?>, offset: Int, length: Int)

Insert a contiguous block of elements supplied as a raw array slice.

Link copied to clipboard
open override fun <T : Any> andThen(p0: Function<in Double, out T>): Function<E, T>
Link copied to clipboard
open fun andThenByte(p0: Double2ByteFunction): Object2ByteFunction<E>
Link copied to clipboard
open fun andThenChar(p0: Double2CharFunction): Object2CharFunction<E>
Link copied to clipboard
open fun andThenDouble(p0: Double2DoubleFunction): Object2DoubleFunction<E>
Link copied to clipboard
open fun andThenFloat(p0: Double2FloatFunction): Object2FloatFunction<E>
Link copied to clipboard
open fun andThenInt(p0: Double2IntFunction): Object2IntFunction<E>
Link copied to clipboard
open fun andThenLong(p0: Double2LongFunction): Object2LongFunction<E>
Link copied to clipboard
open fun <T : Any> andThenObject(p0: Double2ObjectFunction<out T>): Object2ObjectFunction<E, T>
Link copied to clipboard
open fun <T : Any> andThenReference(p0: Double2ReferenceFunction<out T>): Object2ReferenceFunction<E, T>
Link copied to clipboard
open fun andThenShort(p0: Double2ShortFunction): Object2ShortFunction<E>
Link copied to clipboard
open override fun apply(p0: E): Double
Link copied to clipboard
open override fun applyAsDouble(p0: E): Double
Link copied to clipboard
open override fun clear()

Remove all elements and weights from the list.

Link copied to clipboard
open operator override fun compareTo(other: MutableList<out E>): Int
Link copied to clipboard
open fun <V : Any> compose(p0: Function<in V, out E>): Function<V, Double>
Link copied to clipboard
open fun composeByte(p0: Byte2ObjectFunction<E>): Byte2DoubleFunction
Link copied to clipboard
open fun composeChar(p0: Char2ObjectFunction<E>): Char2DoubleFunction
Link copied to clipboard
open fun composeDouble(p0: Double2ObjectFunction<E>): Double2DoubleFunction
Link copied to clipboard
open fun composeFloat(p0: Float2ObjectFunction<E>): Float2DoubleFunction
Link copied to clipboard
open fun composeInt(p0: Int2ObjectFunction<E>): Int2DoubleFunction
Link copied to clipboard
open fun composeLong(p0: Long2ObjectFunction<E>): Long2DoubleFunction
Link copied to clipboard
open fun <T : Any> composeObject(p0: Object2ObjectFunction<in T, out E>): Object2DoubleFunction<T>
Link copied to clipboard
open fun <T : Any> composeReference(p0: Reference2ObjectFunction<in T, out E>): Reference2DoubleFunction<T>
Link copied to clipboard
open fun composeShort(p0: Short2ObjectFunction<E>): Short2DoubleFunction
Link copied to clipboard
open operator override fun contains(element: E): Boolean
Link copied to clipboard
open override fun containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
open override fun containsKey(key: Any?): Boolean

Convenience wrapper that returns whether the given element is contained in the list.

Link copied to clipboard
open override fun defaultReturnValue(): Double

Getter for the default return value used by getDouble when a key is not found.

open override fun defaultReturnValue(rv: Double)

Setter for the default return value used by getDouble when a key is not found.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Equality considers:

Link copied to clipboard
open operator override fun get(p0: Any): Double
open operator override fun get(index: Int): E
Link copied to clipboard
open override fun getDouble(key: Any?): Double

Return the weight associated with key (first occurrence), or the configured defaultReturnValue if the key is not present.

Link copied to clipboard
open override fun getElements(p0: Int, p1: Array<Any>, p2: Int, p3: Int)
Link copied to clipboard
open fun getOrDefault(p0: Any, p1: Double): Double
open override fun getOrDefault(p0: Any, p1: Double): Double
Link copied to clipboard
open override fun hashCode(): Int

Compute hash code consistent with equals: combines items, weights, bounds flags and default return value.

Link copied to clipboard
open override fun indexOf(element: E): Int
Link copied to clipboard
open override fun isEmpty(): Boolean
Link copied to clipboard
open operator override fun iterator(): ObjectListIterator<E>

Delegates to listIterator().

Link copied to clipboard
open override fun lastIndexOf(element: E): Int
Link copied to clipboard
open override fun listIterator(): ObjectListIterator<E>

Delegates to listIterator(0).

open override fun listIterator(index: Int): ObjectListIterator<E>

Return a fail-safe iterator over the list elements (a custom ObjectListIterator).

Link copied to clipboard
open fun put(p0: E, p1: Double): Double
open override fun put(p0: E, p1: Double): Double
Link copied to clipboard
open override fun remove(p0: Any): Double

open override fun remove(element: E): Boolean

Remove the first occurrence of element from the list (and its weight).

Link copied to clipboard
open override fun removeAll(elements: Collection<E>): Boolean

Removes all elements that are contained in the given collection.

Link copied to clipboard
open override fun removeAt(index: Int): E

Remove and return the element at index, also removing the corresponding weight.

Link copied to clipboard
open fun removeDouble(p0: Any): Double
Link copied to clipboard
open override fun removeElements(from: Int, to: Int)

Remove a range of elements [from, to) and corresponding weights.

Link copied to clipboard
open override fun removeIf(filter: Predicate<in E>): Boolean

Removes elements matching the given predicate.

Link copied to clipboard
open override fun replaceAll(operator: UnaryOperator<E>)

Replace every element by applying the given operator and update weights accordingly.

Link copied to clipboard
open override fun retainAll(elements: Collection<E>): Boolean

Retains only elements that are contained in the given collection.

Link copied to clipboard
open operator override fun set(index: Int, element: E): E

Replace the element at index with element, updating its weight as computed by weighter.

Link copied to clipboard
open fun setElements(p0: Array<E>)
open fun setElements(p0: Int, p1: Array<E>)
open fun setElements(p0: Int, p1: Array<E>, p2: Int, p3: Int)
Link copied to clipboard
open fun size(): Int

open override fun size(size: Int)

Resize the list to the given size (shrinking only).

Link copied to clipboard
open override fun sort(c: Comparator<in E>?)

Not supported. Sorting by element comparator is not allowed because weights determine element order.

Link copied to clipboard
open override fun spliterator(): ObjectSpliterator<E>
Link copied to clipboard
open override fun subList(from: Int, to: Int): ObjectList<E>

Return an unmodifiable subList view of the underlying items between [from, to).

Link copied to clipboard
open override fun toString(): String

Return a string representation containing each element and its weight in order as element(weight).

Link copied to clipboard
open fun unstableSort(p0: Comparator<in E>)