mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-22 23:50:06 +08:00
update FlowGraphic
This commit is contained in:
parent
bf1ebebb93
commit
28de77dc79
@ -1,3 +1,4 @@
|
|||||||
|
import cn.tursom.core.usingNanoTime
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
data class Flow(
|
data class Flow(
|
||||||
@ -61,6 +62,13 @@ class Edge {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
repeat(2) {
|
||||||
|
flow[it] = 0
|
||||||
|
flowCache[it] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Edge(flow=${flow.contentToString()}, flowCache=${flowCache.contentToString()})"
|
return "Edge(flow=${flow.contentToString()}, flowCache=${flowCache.contentToString()})"
|
||||||
}
|
}
|
||||||
@ -75,6 +83,9 @@ class Graphic(
|
|||||||
) = GraphicBuilder().also(builder).build()
|
) = GraphicBuilder().also(builder).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DslMarker
|
||||||
|
annotation class Builder
|
||||||
|
|
||||||
private val edgeList = ArrayList<Edge>()
|
private val edgeList = ArrayList<Edge>()
|
||||||
var input: Int = 0
|
var input: Int = 0
|
||||||
var inputFlowChannel: FlowChannel = FlowChannel.A
|
var inputFlowChannel: FlowChannel = FlowChannel.A
|
||||||
@ -109,12 +120,19 @@ class Graphic(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
edgeList.forEach { edge ->
|
||||||
|
edge.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun result(): List<Pair<Int, Int>> {
|
fun result(): List<Pair<Int, Int>> {
|
||||||
return edgeList.map { it[FlowChannel.A] to it[FlowChannel.B] }
|
return edgeList.map { it[FlowChannel.A] to it[FlowChannel.B] }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator(): Iterator<Edge> = edgeList.iterator()
|
override fun iterator(): Iterator<Edge> = edgeList.iterator()
|
||||||
|
|
||||||
|
@Builder
|
||||||
class GraphicBuilder {
|
class GraphicBuilder {
|
||||||
var precision: Int = 8
|
var precision: Int = 8
|
||||||
val edgeMap = ArrayList<EdgeBuilder>()
|
val edgeMap = ArrayList<EdgeBuilder>()
|
||||||
@ -134,6 +152,7 @@ class Graphic(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Builder
|
||||||
class EdgeBuilder(private val id: Int) {
|
class EdgeBuilder(private val id: Int) {
|
||||||
private val flowList = ArrayList<FlowBuilder>()
|
private val flowList = ArrayList<FlowBuilder>()
|
||||||
fun init(graphic: Graphic) {
|
fun init(graphic: Graphic) {
|
||||||
@ -156,6 +175,7 @@ class Graphic(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Builder
|
||||||
data class FlowBuilder(
|
data class FlowBuilder(
|
||||||
val source: Int,
|
val source: Int,
|
||||||
val sourceFlowChannel: Int,
|
val sourceFlowChannel: Int,
|
||||||
@ -171,17 +191,58 @@ class FlowExample {
|
|||||||
graphic.precision = 8
|
graphic.precision = 8
|
||||||
|
|
||||||
var changed = true
|
var changed = true
|
||||||
var it = 0
|
var step = 0
|
||||||
while (changed) {
|
while (changed) {
|
||||||
it++
|
step++
|
||||||
graphic.calc()
|
graphic.calc()
|
||||||
changed = graphic.changed()
|
changed = graphic.changed()
|
||||||
println("step $it changed $changed")
|
println("step $step changed $changed")
|
||||||
graphic.finishCalc()
|
graphic.finishCalc()
|
||||||
println(graphic.result())
|
println(graphic.result())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 性能测试
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testPerformance() {
|
||||||
|
val graphic = getTestGraphic()
|
||||||
|
|
||||||
|
// 热车
|
||||||
|
graphic.precision = 30
|
||||||
|
repeat(1000) {
|
||||||
|
graphic.clear()
|
||||||
|
var changed = true
|
||||||
|
while (changed) {
|
||||||
|
graphic.calc()
|
||||||
|
changed = graphic.changed()
|
||||||
|
graphic.finishCalc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试
|
||||||
|
repeat(5) {
|
||||||
|
intArrayOf(8, 12, 16, 20, 24, 28).forEach { precision ->
|
||||||
|
graphic.clear()
|
||||||
|
graphic.precision = precision
|
||||||
|
|
||||||
|
var step = 0
|
||||||
|
var changed = true
|
||||||
|
val usingTime = usingNanoTime {
|
||||||
|
while (changed) {
|
||||||
|
step++
|
||||||
|
graphic.calc()
|
||||||
|
changed = graphic.changed()
|
||||||
|
graphic.finishCalc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("precision $precision step $step using $usingTime us")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getTestGraphic() = Graphic {
|
fun getTestGraphic() = Graphic {
|
||||||
// 0
|
// 0
|
||||||
edge {
|
edge {
|
||||||
|
Loading…
Reference in New Issue
Block a user