Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

SparseArray as sequence returns list which doesn't match its size #240

Closed
@Dragas

Description

@Dragas
private class SparseArraySequence<T>(private val a: SparseArray<T>) : Sequence<T> {
    override fun iterator() = SparseArrayIterator()

    private inner class SparseArrayIterator : Iterator<T> {
        private var index = 0
        private val size = a.size()

        override fun hasNext() = size > index

        override fun next(): T {
            if (a.size() != size) throw ConcurrentModificationException()
            return a.get(index++)
        }
    }
}

As it currently is, ⁠⁠⁠⁠toSequence()⁠⁠⁠⁠ seems to only return values with positive indices, while returning the original size

As a result, you cannot use ⁠⁠⁠⁠filter(aggregate)⁠⁠⁠⁠ on such sparse array sequences unless you specify that ⁠⁠⁠⁠it⁠⁠⁠⁠ might be null.

Instead of get(index++) it should use valueAt(index++)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions