All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.ormr.fuzzywuzzy.internal.PriorityQueue.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.ormr.fuzzywuzzy.internal

@Suppress("UNCHECKED_CAST")
internal class PriorityQueue(
    initialCapacity: Int = 10,
    private val comparator: Comparator? = null,
) : Collection {
    public override var size: Int = 0
        private set
    private var array: Array = Array?>(initialCapacity) { null } as Array

    fun add(element: T) {
        if (size + 1 == array.size) {
            resize()
        }
        array[++size] = element
        swim(size)
    }

    fun peek(): T? = when {
        isEmpty() -> throw NoSuchElementException()
        else -> array[1]
    }

    fun poll(): T? {
        if (isEmpty()) return null
        val res = peek()
        array.swap(1, size--)
        sink(1)
        array[size + 1] = null
        if ((isNotEmpty()) && (size == (array.size - 1) / 4)) {
            resize()
        }
        return res
    }

    private fun swim(n: Int) {
        array.swim(n, comparator)
    }

    private fun sink(n: Int) {
        array.sink(n, size, comparator)
    }

    private fun resize() {
        val old = array
        array = Array?>(size * 2) { null } as Array
        old.copyInto(array, endIndex = size + 1)
    }

    @Suppress("ReplaceSizeZeroCheckWithIsEmpty") // ????
    override fun isEmpty(): Boolean = size == 0

    override fun contains(element: T): Boolean {
        for (obj in this) {
            if (obj == element) return true
        }
        return false
    }

    override fun containsAll(elements: Collection): Boolean {
        for (element in elements) {
            if (!contains(element)) return false
        }
        return true
    }

    override fun iterator(): Iterator = array.copyOfRange(1, size + 1).map { it!! }.iterator()
}

@Suppress("UNCHECKED_CAST")
private fun  Array.greater(
    i: Int,
    j: Int,
    comparator: Comparator? = null,
): Boolean = when (comparator) {
    null -> {
        val left = this[i]!! as Comparable
        left > this[j]!!
    }
    else -> comparator.compare(this[i]!!, this[j]!!) > 0
}

private fun  Array.sink(a: Int, size: Int, comparator: Comparator? = null) {
    var k = a
    while (2 * k <= size) {
        var j = 2 * k
        if (j < size && this.greater(j, j + 1, comparator)) j++
        if (!this.greater(k, j, comparator)) break
        swap(k, j)
        k = j
    }
}

private fun  Array.swim(size: Int, comparator: Comparator? = null) {
    var n = size
    while (n > 1 && greater(n / 2, n, comparator)) {
        swap(n, n / 2)
        n /= 2
    }
}

private fun  Array.swap(i: Int, j: Int) {
    val tmp = this[i]
    this[i] = this[j]
    this[j] = tmp
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy