Issue Summary: The method checkIndex(index: Int) in the class IndexTreeList (version 3.1.0) appears to enforce an index boundary check that aligns with the contract for AbstractList.set(index, element), but not with that of add(E e) or addFirst(E e).
Code Reference:
fun checkIndex(index: Int) {
if (index < 0 || index >= size)
throw IndexOutOfBoundsException()
}
According to the Java documentation, the index validation for add(int index, E element) (and by extension for similar insertion methods such as addFirst(E e)) should be:
IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size())
The issue manifests when calling addFirst(E e) on an empty IndexTreeList. Internally, this results in a call to checkIndex(0), which throws IndexOutOfBoundsException, even though 0 is a valid index for insertion into an empty list (where size == 0).