Skip to content

[Util/CollectionOperations] Implement maxes-like functions #41

@lounres

Description

@lounres
public fun <T: Comparable<T>> List<T>.maxes(count: Int): List<T> = buildList(count) {
    val iterator = iterator()
    repeat(count) {
        if (!iterator.hasNext()) throw NoSuchElementException()
        this.add(iterator.next())
    }
    while (iterator.hasNext()) {
        val e = iterator.next()
        var index = count - 1
        this[index] = e
        while (index >= 1) {
            if (this[index-1] >= e) break
            this[index] = this[index-1]
            this[index-1] = e
            index--
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions