loggersoft.kotlin.utils.graph.MutableGraphImpl.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
import java.util.NoSuchElementException
internal class MutableGraphImpl : GraphImpl, MutableGraph {
constructor(vararg values: V) {
for(value in values) insertVertex(value)
}
constructor(vararg edges: Edge): super(*edges)
internal constructor(graph: Graph): super(graph)
override fun clone(): MutableGraph = MutableGraphImpl(this)
override fun addVertex(value: V): Vertex = insertVertex(value)
override fun removeVertex(value: V) {
with(getVertex(value)) { startsIn + endsIn }.forEach(::removeEdge)
vertexesMap.remove(value)
}
override fun addEdge(edge: Edge): Edge = insertEdge(edge)
override fun removeEdge(edge: Edge) {
if (!getVertex(edge.startFrom).startsIn.remove(edge) || !getVertex(edge.endWith).endsIn.remove(edge)) throw NoSuchElementException()
if (edgesCount > 0) edgesCount--
if (edge.isOriented && orientedEdgesCount > 0) orientedEdgesCount--
if (edge.isDynamicWeight && dynamicEdgesCount > 0) dynamicEdgesCount--
}
override fun clear() {
vertexesMap.clear()
edgesCount = 0
orientedEdgesCount = 0
dynamicEdgesCount = 0
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy