add
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 to insert
Insert element at the specified index while preserving the sorted-by-weight invariant.
Preconditions:
indexmust 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 atindex(if index < size) to preserve non-decreasing order.
Parameters
insertion index
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