Skip to content

Commit

Permalink
perf(selector): 优化 a<0 遍历逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 15, 2023
1 parent de2d99b commit 56949c2
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package li.songe.selector.data

import li.songe.selector.NodeSequenceFc
import li.songe.selector.util.filterIndexes

/**
* an+b
Expand Down Expand Up @@ -65,15 +64,15 @@ data class PolynomialExpression(val a: Int = 0, val b: Int = 1) : ConnectExpress
override val isConstant = numbers.size == 1
override val minOffset = (numbers.firstOrNull() ?: 1) - 1
private val b1 = b - 1
private val indexes = numbers.map { x -> x - 1 }
private val maxAb = a + b // when a<=0

override val traversal = object : NodeSequenceFc {
override fun <T> invoke(sq: Sequence<T?>): Sequence<T?> {
return if (a > 0) {
sq.filterIndexed { x, _ -> (x - b1) % a == 0 && (x - b1) / a > 0 }
return (if (a > 0) {
sq
} else {
sq.filterIndexes(indexes)
}
sq.take(maxAb)
}).filterIndexed { x, _ -> (x - b1) % a == 0 && (x - b1) / a > 0 }
}
}

Expand Down

0 comments on commit 56949c2

Please sign in to comment.