WeightedSortedList
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 weightConstructors
Convenience constructor that creates empty internal storage with the given default capacity.
Properties
Functions
Add all elements from the given collection while preserving the sorted-by-weight invariant.
Insert a contiguous block of elements at index.
Insert a contiguous block of elements supplied as a raw array slice.
Convenience wrapper that returns whether the given element is contained in the list.
Getter for the default return value used by getDouble when a key is not found.
Setter for the default return value used by getDouble when a key is not found.
Delegates to listIterator(0).
Return a fail-safe iterator over the list elements (a custom ObjectListIterator).
Removes all elements that are contained in the given collection.
Remove a range of elements [from, to) and corresponding weights.
Replace every element by applying the given operator and update weights accordingly.
Retains only elements that are contained in the given collection.
Not supported. Sorting by element comparator is not allowed because weights determine element order.