Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lounres opened this issue Mar 22, 2023 · 0 comments
Open

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

lounres opened this issue Mar 22, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@lounres
Copy link
Owner

lounres commented Mar 22, 2023

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--
        }
    }
}
@lounres lounres added the enhancement New feature or request label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant