add

open override fun add(element: E): Boolean

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

  • Computes the element's weight via weighter.

  • If the weight is out of the configured bounds, the element is not inserted and the method returns false.

  • Otherwise, finds the insertion index (binary search) and inserts the element and its weight.

Return

true if the element was inserted, false if its weight is out of bounds

Complexity: O(n) due to potential shifting of elements; O(log n) to find insertion index.

Parameters

element

element to insert


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

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

Preconditions:

  • index must be a valid insertion index (0 <= index <= size).

  • The computed weight must be within bounds.

  • The weight must be >= weight at index - 1 (if index 0) and <= weight at index (if index < size) to preserve non-decreasing order.

Parameters

index

insertion index

element

element to insert

Throws

if index is invalid

if the element's weight is out of bounds or inserting at index would break sorted order