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

loggersoft.kotlin.utils.graph.Edge.kt Maven / Gradle / Ivy

/*
 * Copyright (C) 2018 Alexander Kornilov ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package loggersoft.kotlin.utils.graph

/**
 * Represents interface of a graph's edge based on [AbstractEdge].
 *
 * @author Alexander Kornilov ([email protected]).
 */
interface Edge : AbstractEdge {

    companion object {

        /**
         * A default value of the edge weight.
         */
        const val DefaultWeight: Double = 1.0
    }

    /**
     * Vertex where edge starts from.
     */
    val startFrom: V

    /**
     * Vertex where edge ends with.
     */
    val endWith: V

    /**
     * An edge parametrized value.
     */
    val value: E

    /**
     * [startFrom]
     */
    operator fun component1() = startFrom

    /**
     * [endWith]
     */
    operator fun component2() = endWith

    /**
     * [value]
     */
    operator fun component3() = value

    /**
     * Returns weight or [default] if edges don't have weight.
     */
    fun weightOrDefault(default: Double = DefaultWeight) = if (hasWeight) weight else default

    /**
     * Returns opposite vertex for [startFrom] or [endWith] values.
     * @throws NoSuchElementException
     */
    fun opposite(value: V): V = when (value) {
        startFrom -> endWith
        endWith -> startFrom
        else -> throw NoSuchElementException()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy