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

kotlin.concurrent.FunctionalQueue.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
package kotlin.concurrent

import java.util.concurrent.Executor

class FunctionalQueue (
    val input: FunctionalList = FunctionalList.emptyList(),
    val output: FunctionalList = FunctionalList.emptyList()) {

    val size : Int
        get() = input.size + output.size

    val empty : Boolean
        get() = size == 0

    public fun add(element: T) : FunctionalQueue = FunctionalQueue(input add element, output)

    public fun addFirst(element: T) : FunctionalQueue = FunctionalQueue(input, output add element)

    public fun removeFirst() : Pair> =
        if(output.empty) {
            if(input.empty)
                throw java.util.NoSuchElementException()
            else
                FunctionalQueue(FunctionalList.emptyList(), input.reversed()).removeFirst()
        }
        else {
            Pair(output.head, FunctionalQueue(input, output.tail))
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy