Skip to content

Commit

Permalink
perf: binaryExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 26, 2024
1 parent d9c5677 commit 0ac0b69
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions app/src/main/kotlin/li/songe/gkd/service/AbExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ val allowPropertyNames by lazy {
}

fun Selector.checkSelector(): String? {
nameToTypeList.forEach { (name, type) ->
if (!allowPropertyNames.contains(name)) {
return "未知属性:${name}"
binaryExpressions.forEach { e ->
if (!allowPropertyNames.contains(e.name)) {
return "未知属性:${e.name}"
}
if (type != "null" && allowPropertyNames[name] != type) {
return "非法类型:${name}=$type"
if (e.value.type != "null" && allowPropertyNames[e.name] != e.value.type) {
return "非法类型:${e.name}=${e.value.type}"
}
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class MultiplatformSelector private constructor(
val qfTextValue = selector.qfTextValue
val canQf = selector.canQf
val isMatchRoot = selector.isMatchRoot
val nameToTypeList = selector.nameToTypeList

// [name,operator,value][]
val binaryExpressions = selector.binaryExpressions.map { e ->
arrayOf(
e.name,
e.operator.key,
e.value.type,
e.value.toString()
)
}.toTypedArray()

fun <T : Any> match(node: T, transform: MultiplatformTransform<T>): T? {
return selector.match(node, transform.transform)
Expand Down
14 changes: 4 additions & 10 deletions selector/src/commonMain/kotlin/li/songe/selector/Selector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,21 @@ class Selector internal constructor(private val propertyWrapper: PropertyWrapper
keys.toTypedArray()
}

private val binaryExpressions = run {
val binaryExpressions = run {
var p: PropertyWrapper? = propertyWrapper
val names = mutableListOf<BinaryExpression>()
val expressions = mutableListOf<BinaryExpression>()
while (p != null) {
val s = p.propertySegment
names.addAll(s.binaryExpressions)
expressions.addAll(s.binaryExpressions)
p = p.to?.to
}
names.distinct().toTypedArray()
expressions.distinct().toTypedArray()
}

val propertyNames = run {
binaryExpressions.map { e -> e.name }.distinct().toTypedArray()
}


// [name,type][]
val nameToTypeList = run {
binaryExpressions.map { e -> arrayOf(e.name, e.value.type) }.distinct().toTypedArray()
}

val canCacheIndex =
connectKeys.contains(ConnectOperator.BeforeBrother.key) || connectKeys.contains(
ConnectOperator.AfterBrother.key
Expand Down

0 comments on commit 0ac0b69

Please sign in to comment.